You can use the replicate() function to repeatedly evaluate some expression in R a certain number of times.
This function uses the following basic syntax:
replicate(n, expr)
where:
- n: The number of times to repeatedly evaluate some expression.
- expr: The expression to evaluate.
The following examples show how to use this function in practice.
Example 1: Replicate a Value Multiple Times
The following code shows how to use the replicate() function to repeatedly evaluate a single value multiple times:
#replicate the value 3 exactly 10 times replicate(n=10, 3) [1] 3 3 3 3 3 3 3 3 3 3 #replicate the letter 'A' exactly 7 times replicate(n=7, 'A') [1] "A" "A" "A" "A" "A" "A" "A" #replicate FALSE exactly 5 times replicate(n=5, FALSE) [1] FALSE FALSE FALSE FALSE FALSE
Example 2: Replicate a Function Multiple Times
Now suppose we’d like to repeatedly evaluate some function.
For example, suppose we use the rnorm() function to produce three values for a random variable that follows a normal distribution with a mean of 0 and a standard deviation of 1:
#make this example reproducible
set.seed(1)
#generate 3 values that follow normal distribution
rnorm(3, mean=0, sd=1)
[1] -0.6264538 0.1836433 -0.8356286
Using the replicate() function, we can repeatedly evaluate this rnorm() function a certain number of times.
For example, we can evaluate this function 5 times:
#make this example reproducible
set.seed(1)
#generate 3 values that follow normal distribution (replicate this 4 times)
replicate(n=4, rnorm(3, mean=0, sd=1))
[,1] [,2] [,3] [,4]
[1,] 1.5952808 0.4874291 -0.3053884 -0.6212406
[2,] 0.3295078 0.7383247 1.5117812 -2.2146999
[3,] -0.8204684 0.5757814 0.3898432 1.1249309
The result is a matrix with 3 rows and 4 columns.
Or perhaps we’d like to evaluate this function 6 times:
#make this example reproducible
set.seed(1)
#generate 3 values that follow normal distribution (replicate this 6 times)
replicate(n=6, rnorm(3, mean=0, sd=1))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1.5952808 0.4874291 -0.3053884 -0.6212406 -0.04493361 0.8212212
[2,] 0.3295078 0.7383247 1.5117812 -2.2146999 -0.01619026 0.5939013
[3,] -0.8204684 0.5757814 0.3898432 1.1249309 0.94383621 0.9189774
The result is a matrix with 6 rows and 3 columns.
Using replicate() to Simulate Data
The replicate() function is particularly useful for running simulations.
For example, suppose we’d like to generate 5 samples of size n = 10 that each follow a normal distribution.
We can use the replicate() function to produce 5 different samples and we can then use the colMeans() function to find the mean value of each sample:
#make this example reproducible set.seed(1) #create 5 samples each of size n=10 data 0, sd=1)) #view samples data [,1] [,2] [,3] [,4] [,5] [1,] -0.6264538 1.51178117 0.91897737 1.35867955 -0.1645236 [2,] 0.1836433 0.38984324 0.78213630 -0.10278773 -0.2533617 [3,] -0.8356286 -0.62124058 0.07456498 0.38767161 0.6969634 [4,] 1.5952808 -2.21469989 -1.98935170 -0.05380504 0.5566632 [5,] 0.3295078 1.12493092 0.61982575 -1.37705956 -0.6887557 [6,] -0.8204684 -0.04493361 -0.05612874 -0.41499456 -0.7074952 [7,] 0.4874291 -0.01619026 -0.15579551 -0.39428995 0.3645820 [8,] 0.7383247 0.94383621 -1.47075238 -0.05931340 0.7685329 [9,] 0.5757814 0.82122120 -0.47815006 1.10002537 -0.1123462 [10,] -0.3053884 0.59390132 0.41794156 0.76317575 0.8811077 #calculate mean of each sample colMeans(data) [1] 0.1322028 0.2488450 -0.1336732 0.1207302 0.1341367
From the output we can see:
- The mean of the first sample is 0.1322.
- The mean of the second sample is 0.2488.
- The mean of the third sample is -0.1337.
And so on.
Additional Resources
How to Select Specific Columns in R
How to Drop Columns from Data Frame in R
How to Remove Rows from Data Frame Based on Condition in R