How to simulate 50 random samples and calculate mean and variance of each sample

When I simulate 50 random samples of a normal distribution and try to calculate the mean and the variance of each simulation I got this error: "There were 50 or more warnings (use warnings() to see the first 50)".

    n=100
    mean=100
    sd=25 

    sample=NULL
    meansample=NULL
    sdsample=NULL

    for (i in 1:50)
      { 
    sample[i]=rnorm(n,mean,sd)
    meansample[i]=mean(sample[i])
    sdsample[i]=sd(sample[i])

    }

    sample
    meansample
    sdsample`

I want to ask how do I calculate correctly the mean and the standard deviation and why I get this error "There were 50 or more warnings (use warnings() to see the first 50)" when I execute my code.

This is not an error message, it is a warning message that is telling you that even more warnings exist and you should run warnings() to see them.

You are getting this warnings because you are trying to replace 1 value sample[i] by 100 values rnorm(100,100,25)

sample=NULL

sample[1] = rnorm(100,100,25)
#> Warning in sample[1] = rnorm(100, 100, 25): number of items to replace is
#> not a multiple of replacement length

sample = rnorm(100,100,25)
sample
#>   [1] 146.99734 100.04879 125.79258  61.62826 126.44220  74.28412 109.55534
#>   [8] 101.99495 104.04697  94.50560  82.96956 120.45511  91.71064  54.55138
#>  [15] 100.44118  74.78116  67.91052 120.90550 104.24655 129.60374  99.18980
#>  [22] 120.90027 102.83226  57.69101 108.13859  64.85401  95.71069 165.83093
#>  [29]  99.59100 155.57403  86.61336  99.58995 125.77868 123.88675 189.76440
#>  [36]  98.63245  85.83580 105.60787 130.22156  78.52931 118.96382 121.20032
#>  [43] 112.86292  93.19341  65.74258 101.88642 100.39258  68.42377  69.81394
#>  [50]  62.30854 123.82010 136.12220 132.01335 132.13291 132.65515  58.31555
#>  [57] 107.22115  91.40452  69.90416 120.65045  99.72977 104.37482 105.79507
#>  [64] 138.54985 117.64421  94.13152 121.05296 122.43424  87.00925 111.82037
#>  [71]  92.84810  87.95937 117.55196 106.15243 110.59320 103.03996  97.51630
#>  [78]  85.96385  89.74359  84.73433 172.00426 115.55229  92.62782  64.41190
#>  [85] 111.25803 162.48153 105.60092  83.40296  61.97425 127.71648  94.82223
#>  [92]  85.37024 125.29502 139.77201 117.49552  67.99834 105.20246  89.62526
#>  [99] 100.31425  98.27988

Thank you for your fast reply.

I understand what you say, but do you know how can I repeat the rnorm(100,100, 25) 50 diferent times and "store" each time in separate variables (for example sample[1] , sample[2] , .... , sample[50])? I cant do that yet.

Thank you

This would be one way to do it

sample <- purrr::map_dfc(1:50, ~ list(sample = rnorm(100,100,25)))
sample
#> # A tibble: 100 x 50
#>    sample sample1 sample2 sample3 sample4 sample5 sample6 sample7 sample8
#>     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1   88.2   129.     84.6   137.    107.    139.     75.4   120.     82.0
#>  2  108.    118.     95.3   126.     93.2    80.3    62.2    99.7   128. 
#>  3   89.0    88.1    89.6   127.    106.     88.0   106.    149.    108. 
#>  4  108.     90.2   115.    112.     93.8   102.    101.     93.8   120. 
#>  5  105.    106.     78.9    91.7    95.3    98.4    70.9   122.     85.3
#>  6   95.9    85.0   112.    133.     73.8   128.    126.     83.9    97.7
#>  7  126.    163.    116.    120.     63.6    75.1    92.9    86.3   112. 
#>  8   57.5   142.    119.     95.8   103.    121.    114.    117.    100. 
#>  9  128.     91.1    94.1   115.    141.     88.6   103.     91.0    65.8
#> 10   54.6   123.     87.5   111.    149.    163.    119.     91.8   108. 
#> # … with 90 more rows, and 41 more variables: sample9 <dbl>,
#> #   sample10 <dbl>, sample11 <dbl>, sample12 <dbl>, sample13 <dbl>,
#> #   sample14 <dbl>, sample15 <dbl>, sample16 <dbl>, sample17 <dbl>,
#> #   sample18 <dbl>, sample19 <dbl>, sample20 <dbl>, sample21 <dbl>,
#> #   sample22 <dbl>, sample23 <dbl>, sample24 <dbl>, sample25 <dbl>,
#> #   sample26 <dbl>, sample27 <dbl>, sample28 <dbl>, sample29 <dbl>,
#> #   sample30 <dbl>, sample31 <dbl>, sample32 <dbl>, sample33 <dbl>,
#> #   sample34 <dbl>, sample35 <dbl>, sample36 <dbl>, sample37 <dbl>,
#> #   sample38 <dbl>, sample39 <dbl>, sample40 <dbl>, sample41 <dbl>,
#> #   sample42 <dbl>, sample43 <dbl>, sample44 <dbl>, sample45 <dbl>,
#> #   sample46 <dbl>, sample47 <dbl>, sample48 <dbl>, sample49 <dbl>
1 Like

Thank you. It works :slight_smile:

If you are not focused only on tidyverse solutions, you can use replicate:

replicate(50, rnorm(100, 100, 25))

In this case, you will get a matrix of dimension 100 x 50, i.e. each sample of 100 observations will be placed in a new column.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.