Hey, readers! In this article, we will be focusing on rnorm() function in R programming in detail.
So, let us begin!!
Table of Contents
Functioning of rnorm() function in R
Before diving deep into the concept of rnorm() function, let us understand few things about the Normal Distribution of data.
To begin with, data distribution helps us understand the variation of the data and its values with respect to the factors.
Normal distribution has the data distributed normally i.e. it follows a uniform (bell-curved shaped) segregation of data. It happens to have the mean value as 1 and standard variance as constant.
At times, we feel the need to generate numbers at random to test the functionality of the steps of other effects on the data.
rnorm() function, we can generate random numbers that follow normal distribution of data at ease. Unlike other functions, rnorm() function provides us with the facility to customize the value of mean and variance accordingly.
rnorm(num, mean, variance)
We need to provide the rnorm() function with the number of values that needs to be generated. Further, the mean and variance values are optional. If not provided, it takes the default values as mean = 0 and variance = 1.
Let us now focus on the implementation of the same!
Recommended read – How to normalize data in R?
Example 1: R rnorm() function with a range of values
In this example, we have passed a range of values from 1 to 10 for the rnorm() function to frame the normalized values. Further, we have made use of the default values of mean and variance here.
#Removed all the existing objects rm(list = ls()) dta = rnorm(1:10) print(dta)
> print(dta)  0.7888985 -1.5486737 0.7559353 1.2456296 -1.7645367 -1.3447497 0.5142277 -0.6125222 -0.9832453 -0.4619408
Example 2: R rnorm() function with customized values of mean
Here, we have now passed a range of 10 for the rnorm() function to create normalized values. Further, we have passed the mean as 2.2, which means all the normalized values created would cumulatively have a mean of 2.2
#Removed all the existing objects rm(list = ls()) dta = rnorm(1:10,2.2) print(dta) summary(dta)
> print(dta)  1.682294 3.132749 2.890573 1.049677 1.902492 2.794267 3.170417 2.046062 2.376102 1.632624 > summary(dta) Min. 1st Qu. Median Mean 3rd Qu. Max. 1.050 1.737 2.211 2.268 2.866 3.170
Using summary() function, we have cross checked the presence of mean of the formed normalized values to be equal to 2.2. As the variance value is not mentioned, it would take the default value i.e. 1.
Example 3: R rnorm() function with customized values of variance
Apart from mean, we can separately customize the value variance for the random normalized values created.
#Removed all the existing objects rm(list = ls()) dta = rnorm(1:5, ,3) print(dta) summary(dta)
> print(dta)  -0.7515998 -5.0313071 -2.3954319 2.1552616 -0.6741772 > summary(dta) Min. 1st Qu. Median Mean 3rd Qu. Max. -5.0313 -2.3954 -0.7516 -1.3395 -0.6742 2.1553
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to R programming, stay tuned.
Till then, Happy Learning!! 🙂
Reference – Official documentation