The rnorm() function in R generates a random number using a normal(bell curve) distribution. Thus, the rnorm() function simulates random variates having a specified normal distribution.
What is Normal Distribution in R
The normal distribution is the collection of random data from independent sources. We get the bell shape curve by plotting a graph with the variable’s value on the horizontal axis and the values’ count on the vertical axis. The center of the curve represents the mean of the dataset.
The normal distributions behave differently in the different scopes.
- The normal distribution in the rmult scope is the usually known multivariate joint normal distribution.
- The normal distribution in the rplus space has to be somehow truncated at 0.
- The normal distribution of rcomp is observed as a normal distribution within the simplex as a geometrical portion of the real vector space.
- The normal distribution of acomp and aplus is defined in them as in the real space because they are metric vector spaces.
Densities are only given for the models built for rmult, rplus, and acomp because they do exist w concerning the Lebesgue measure of each of these spaces.
Functions in R for Probability Distributions
R distribution handles four functions to generate random numbers. First, there is a root name. The root name for the normal distribution is the norm. This root is prefixed by one of the following letters.
- The p is for “probability“, and it is a cumulative distribution function. For example, pnorm() function.
- The q is for “quantile“, and it is an inverse. For example, qnorm() function.
- The d is for “density“, which is a density function. For example, dnorm() function.
- The r is for “random“, and it is a random variable having the specified distribution. For example, rnorm() function.
We will talk about rnorm() in this tutorial. As with pnorm(), qnorm(), and dnorm(), the optional arguments define the mean and standard deviation of the distribution.
The rnorm in R is a built-in function that generates a vector of normally distributed random numbers. The rnorm() function takes a sample size as input and generates many random numbers.
It generates the Normal Distributions On Special Spaces. The rnorm() function helps to generate random numbers whose distribution is normal.
rnorm(n, mean, sd)
n: It is the number of observations(sample size).
mean: It is the mean value of the sample data. Its default value is zero.
sd: It is the standard deviation. Its default value is 1.
data <- rnorm(10) data
 -0.359721535 1.516744916 -0.380787719 0.345410241 0.321703671 -0.436644645 -0.267328311  -1.640269174 -0.190012636 -0.004461941
Create a histogram based on rnorm()
To create a histogram in R, use the hist() function. A histogram depicts the frequencies of values of a variable bucketed into ranges. The histogram is similar to the bar chart, but it groups the values into continuous ranges.
To create a normal distribution in R, use the rnorm() function. Let’s generate 30 random numbers using the rnorm() function and create a histogram based on that distribution.
data <- rnorm(30) hist(data, main="Normal distribution")
And we get the bar chart type of histogram based on the random data.
Generate three different vectors of random numbers in R
Let’s generate three different vectors of random numbers in R using the rnorm() function.
Let’s generate histograms based on these random numbers.
k10 <- rnorm(10, mean = 50, sd = 8) k100 <- rnorm(100, mean = 50, sd = 8) k1000 <- rnorm(10000, mean = 50, sd = 8) oldpar <- par() par(mfrow=c(1,3)) # The breaks argument specifies how many bars are in the histogram hist(k10, breaks = 5) hist(k100, breaks = 20) hist(k1000, breaks = 80)
You can see that we have created three histograms using three different normal distributions.
rnorm() function with customized values of mean
Let’s find normalized values that cumulatively have a mean of 1.9.
data <- rnorm(1:21, 1.9) print(data) summary(data)
 0.38755150 2.75291745 1.85497073 3.06377678 2.19992803 2.19544549  3.06391987 2.11362968 0.08858567 1.80981794 0.62872918 1.95926405  1.00410509 3.33564571 1.56683186 1.07596651 1.19985883 3.28332289  2.07253467 0.85443979 3.74952206 Min. 1st Qu. Median Mean 3rd Qu. Max. 0.08859 1.07597 1.95926 1.91718 2.75292 3.74952
In this example, we checked that the mean of the formed normalized values is 1.9 using the summary() function. The distributed values are up to 21 because our vector size is 21.
rnorm() function with customized values of variance
Let’s find normalized values that cumulatively have a variance of 2.1.
data <- rnorm(1:19, , 2.1) print(data) summary(data)
 1.23115078 0.31362906 2.58047198 -0.81273081 -1.85073469 -0.40239977  1.36642265 -1.27276158 -0.71489111 -3.27758153 -1.74129457 0.63656383  0.74461485 0.01650333 1.86487174 1.42136404 1.13460518 1.19707156  -1.90298827 Min. 1st Qu. Median Mean 3rd Qu. Max. -3.27758 -1.04275 0.31363 0.02799 1.21411 2.58047
In this example, we checked that the variance of the formed normalized values is 2.1 using the summary() function. The distributed values are up to 19 because our vector size is 19.
Applications of rnorm() function
The normal distribution is vastly used in many areas like
- Businesses analysis.
- Stock market analysis or any trading pattern.
- Sports scores.
- Athletic performance.
To generate a vector of normally distributed random numbers in R, use the rnorm() function. The first argument, n, is the number of numbers you want to generate, followed by the standard mean and sd arguments.
That’s it for this tutorial.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.