You can quickly generate a normal distribution in R by using the rnorm() function, which uses the following syntax:
rnorm(n, mean=0, sd=1)
- n: Number of observations.
- mean: Mean of normal distribution. Default is 0.
- sd: Standard deviation of normal distribution. Default is 1.
This tutorial shows an example of how to use this function to generate a normal distribution in R.
Example: Generate a Normal Distribution in R
The following code shows how to generate a normal distribution in R:
#make this example reproducible set.seed(1) #generate sample of 200 obs. that follows normal dist. with mean=10 and sd=3 data <- rnorm(200, mean=10, sd=3) #view first 6 observations in sample head(data)  8.120639 10.550930 7.493114 14.785842 10.988523 7.538595
We can quickly find the mean and standard deviation of this distribution:
#find mean of sample mean(data)  10.10662 #find standard deviation of sample sd(data)  2.787292
We can also create a quick histogram to visualize the distribution of data values:
We can even perform a Shapiro-Wilk test to see if the dataset comes from a normal population:
shapiro.test(data) Shapiro-Wilk normality test data: data W = 0.99274, p-value = 0.4272
The p-value of the test turns out to be 0.4272. Since this value is not less than .05, we can assume the sample data comes from a population that is normally distributed.
This result shouldn’t be surprising since we generated the data using the rnorm() function, which naturally generates a random sample of data that comes from a normal distribution.