How to Simulate Rnorm for Many Observations Using Different Mean and Sd Values in R

Jinku Hu Feb 02, 2024
  1. Use the Map Function to Simulate rnorm for Many Observations in R
  2. Use the apply Function to Simulate rnorm for Many Observations in R
How to Simulate Rnorm for Many Observations Using Different Mean and Sd Values in R

This article will demonstrate multiple methods of simulating rnorm for many observations using different mean and sd values in R.

Use the Map Function to Simulate rnorm for Many Observations in R

The rnorm function is used to generate random deviates for the normal distribution given the default mean equals 0 and standard deviation(sd) is 1. Note that the latter parameters can be passed optionally as the vector of elements. In this case, we stored predefined mean and sd values as part of the data frame. Map function applies the given function object to the corresponding elements of multiple vectors. It takes the function object as the first argument and vector objects as the following arguments. Notice that, number of vector objects should be equal to the mandatory parameters of the given function object. In the following example, we generate 5 deviates for each data element. Also, we use the set.seed function to specify the seed value for reproducible results between multiple program executions. The Map function returns a list object.

set.seed(123)
df1 <- data.frame(
  data = sample(1:64, 4),
  mean = sample(1:64, 4),
  sd = c(1, 4, 8, 20)
)

n <- 5
func1 <- function(x, y) rnorm(n, mean = x, sd = y)
list1 <- Map(func1, df1$mean, df1$sd)
list1

Output:

[[1]]
[1] 3.129288 4.715065 3.460916 1.734939 2.313147

[[2]]
[1] 40.21735 46.89633 43.43926 43.60309 42.44273

[[3]]
[1] 45.55327 64.29531 53.98280 34.26706 55.61085

[[4]]
[1] 44.54417 32.64353 49.64050 33.47991 39.42218

Use the apply Function to Simulate rnorm for Many Observations in R

Alternatively, we can use the apply function to simulate rnorm for different rows in the data frame. apply function is generally used to return values from applying the given function object to the specified margins of an array or a matrix. Margins are specified using the second parameter named MARGIN. MARGIN argument can have the value of 1, which indicates the function to be applied to the rows of the matrix. On the other hand, value 2 denotes the columns of the matrix, and c(1,2) indicates both - rows and columns of the matrix. The first argument of the apply function can be an array or a matrix. Note, though, if the passed object is not an array, it gets coerced to the array type using as.matrix or as.array functions.

set.seed(123)
df1 <- data.frame(
  data = sample(1:64, 4),
  mean = sample(1:64, 4),
  sd = c(1, 4, 8, 20)
)

n <- 5
func1 <- function(x) rnorm(n, mean = x[1], sd = x[2])
apply(df1[-1], 1, FUN = func1)

Output:

         [,1]     [,2]     [,3]     [,4]
[1,] 3.129288 40.21735 45.55327 44.54417
[2,] 4.715065 46.89633 64.29531 32.64353
[3,] 3.460916 43.43926 53.98280 49.64050
[4,] 1.734939 43.60309 34.26706 33.47991
[5,] 2.313147 42.44273 55.61085 39.42218
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - R Distribution