Built with Tutify.io

    AIStudyLabs

    Explain R rnorm() Function with Examples

    Explain R rnorm() Function with Examples

    The rnorm() function in R is used to generate random numbers from a normal distribution. This function is part of the base R package and is commonly used in statistical simulations and analyses. This function allows users to generate random samples with specific characteristics by customizing parameters such as the mean, standard deviation(sd), and sample size(n). If these parameters are not specified, the function defaults to standard values.

    In this article, I will cover the rnorm() function in R, explaining its syntax and parameters, and demonstrating how to use it to generate random values from a normal distribution. Additionally, I will discuss how to set a seed with the set.seed() function enables the generation of reproducible random values, providing consistent results.

    Key Points-

    <a id="h-r-rnorm-function"></a>

    R rnorm() Function

    The rnorm() function in R generates random numbers from a normal (Gaussian) distribution, which is defined by two key parameters: the mean and the standard deviation. These parameters are essential for determining the shape and spread of the distribution. The mean indicates the central value of the distribution, while the standard deviation measures its dispersion. Together, these statistics are crucial for describing the characteristics of a dataset, particularly within the context of a normal distribution.

    <a id="h-syntax-of-the-rnorm-function"></a>

    Syntax of the rnorm() Function

    Following is the syntax of the rnorm() function.

    
    # Syntax of the rnorm()
    rnorm(n, mean = 0, sd = 1)
    

    <a id="h-parameters"></a>

    Parameters

    <a id="h-return-value"></a>

    Return Value

    The rnorm() function in R returns a vector of random numbers generated from a normal distribution with a specified mean and standard deviation.

    <a id="h-generate-random-numbers-from-normal-distribution"></a>

    Generate Random Numbers from Normal Distribution

    You can use the rnorm() function in R with its default parameters to generate random numbers from a standard normal distribution. By setting the n parameter, you can specify the exact number of random values you want to generate from this distribution.

    Let's specify the n parameter with a specified number to get the random values of a specified size from the distribution.

    
    # Generate random numbers using rnorm()
    print("Generate random values from normal distribution")
    rnorm(5, mean = 0, sd = 1)
    

    Yields below output.

    <a id="h-r-rnorm-with-only-positive-numbers"></a>

    R rnorm with only Positive Numbers

    You can generate positive random values from the normal distribution using the rnorm() function. Since the normal distribution includes negative values, you can simply filter the results to retain only the positive numbers.

    
    # Generate positive random values from normal distribution
    positive_num <- rnorm(10)
    positive_num <- positive_num[positive_num > 0]
    print("Generate positive random values from normal distribution")
    positive_num
    

    Yields below output.

    <a id="h-generate-random-numbers-with-a-specified-mean-and-standard-deviation"></a>

    Generate Random Numbers with a Specified Mean and Standard Deviation

    Generate random values from a normal distribution with a customized mean and standard deviation. You can specify both the mean and sd parameters with your desired values and pass them into the function. The function will then return random samples based on the specified sample size, mean, and standard deviation.

    
    # Generate random numbers with custom parameters
    print("Generate random values with custom parameters")
    rnorm(5, mean = 5, sd = 2)
    
    # Output:
    # [1] "Generate random values with custom parameters"
    # [1] 4.2624183 2.7466223 6.1016088 5.5457809 0.7788721
    

    The above code generates 5 random numbers with a mean of 5 and a standard deviation of 2.

    <a id="h-rnorm-for-a-matrix"></a>

    rnorm for a Matrix

    Alternatively, you can generate a matrix of random numbers by using the matrix() function together with rnorm(). To do this, pass the desired number of random values from rnorm() along with the specified matrix dimensions into the matrix() function.

    
    # Generate a matrix of random numbers
    print("Generate  matrix of random values")
    matrix(rnorm(9), nrow = 3, ncol = 3)
    
    # Output:
    # [1] "Generate  matrix of random values"
    #            [,1]       [,2]       [,3]
    # [1,] -1.6914478 -0.2694524  1.7142403
    # [2,]  1.1366530  0.3213288  0.6698729
    # [3,] -0.5879979  0.6314542 -0.2118091
    

    The above code generates a 3x3 matrix with random numbers from a normal distribution

    <a id="h-r-rnorm-with-a-vector-of-means"></a>

    R rnorm with a Vector of Means

    You can generate random values from normal distributions with varying means by using the sapply() function in combination with the rnorm() function. By applying sapply() to rnorm() with a vector of different means, you can generate multiple sets of random numbers, each drawn from a normal distribution with a unique mean but a consistent standard deviation.

    
    # Generate random numbers with different means
    means <- c(0, 1, 2)
    print("Generate random numbers with different maens:")
    sapply(means, function(m) rnorm(5, mean = m, sd = 1))
    
    # Output:
    # [1] "Generate random numbers with different maens:"
    #             [,1]      [,2]     [,3]
    # [1,] -0.78141880 0.1391238 2.146799
    # [2,]  0.07736725 2.1559213 2.613538
    # [3,]  0.52181434 2.3600167 1.701881
    # [4,] -0.54082489 1.2661839 2.752917
    # [5,] -0.38270252 0.8763458 3.077697
    

    <a id="h-setting-a-seed-with-r-rnorm"></a>

    Setting a seed with R rnorm

    You can generate reproducible random numbers from a normal distribution with a specified mean and standard deviation by setting the seed with a specific value. This makes sure that the random numbers produced will be the same each time the code is run, which is particularly useful in scenarios where reproducibility is important, such as in simulations or testing.

    
    # Generate reproducible random numbers
    set.seed(123)
    print("Generate reproducible random numbers:")
    rnorm(5, mean = 2, sd = 0.5)
    
    # OUtput:
    # [1] "Generate reproducible random numbers:"
    # [1] 1.719762 1.884911 2.779354 2.035254 2.064644
    

    <a id="h-visualize-the-random-numbers"></a>

    Visualize the Random Numbers

    Finally, you can visualize the random samples from the distribution by creating a histogram. A histogram is a type of bar chart that illustrates the distribution of a dataset. This process involves generating a set of random numbers from a normal distribution and then plotting these numbers in a histogram to visualize their distribution.

    
    # Visualize the random numbers
    data <- rnorm(5, mean = 0, sd = 1)
    print("Generate random values:")
    print(data)
    hist(data, breaks = 10, main = "Histogram of Generated Random Values", xlab = "Value", col = "lightblue")
    
    # Output:
    # [1] "Generate random values:"
    # [1]  0.005764186  0.385280401 -0.370660032  0.644376549 -0.220486562
    

    Yields below output.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained the rnorm() function in R is a powerful tool for generating random numbers from a normal distribution. Also covered the syntax, parameters, and usage of the rnorm() function, to demonstrate how to generate random values from a normal distribution. Additionally, I explained how to visualize these values using a histogram and by setting a seed with the set.seed() function, you can provide reproducibility, allowing consistent results across different runs.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References