Generate Normally Distributed Data in R

Your goal

You need to randomly generate normally distributed data in R.

Step-by-step tutorial

We will use the rnorm function from the stats package to do this.

A normal distribution has two parameters: the mean and standard deviation. Let's generate 80 synthetic IQ scores, which have a mean of 100 and standard deviation of 15.

> data <- rnorm(n=80, mean=100, sd=15)
> data
 [1] 107.59739  86.82734 105.75557 140.61689  79.30477  93.38860 117.55668
 [8]  98.16536 111.02870  75.55544  68.45987  88.78460 132.34077 113.10244
[15]  86.44122 116.09055  93.81042 116.36774 102.73155 107.07575 116.61098
[22]  63.03839 106.96146 124.46263 102.36290 108.79479 140.37004  83.53110
[29]  87.56253 102.83071 122.10060 104.41525 108.25807  76.93467 128.92910
[36]  98.94531  96.76483 123.73151  86.51844 101.85017  73.01543  69.72891
[43] 105.03372 122.55754 112.75699  88.33516 132.03959  82.22491 108.03649
[50] 102.60046 107.59528 102.38815 113.60668 113.61359  92.52023 105.35457
[57]  79.39395  93.27841  95.91726  90.28471  96.05231  81.60122  91.55943
[64] 112.37783  95.25819  93.41082 113.31342  79.27209  45.37943 101.68835
[71] 122.14528 108.69253  87.32781  92.47280  95.69944  75.31613  65.95922
[78]  97.84732  85.21377  97.25320

We can use hist to generate a histogram for the distribution:

> hist(data)
Histogram of normally distributed data
Histogram of normally distributed data