The replicate()
function in R creates a repeated expression or function for a specified number of times.
The replicate()
function takes the syntax below:
replicate(x, ...)
The replicate()
function takes the following parameter values:
x
: This is the expression or statement to be evaluated or repeated. It is a required parameter. ...
: This is the number of times for the replication. This is also a required parameter. In the code below, we create a sequence of numbers from 1
to 5
which increments by 1
. This sequence is repeated 5
times:
# A code to illustrate the replicate() function in Rreplicate(5, seq(1,5,1))
In the code below, we use the replicate()
function to repeat a function, rnorm()
. This is used to obtain the normal distribution for a value, 4
, whose mean value is 5
. This will be repeated 5
times:
# A code to illustrate the replicate() functionreplicate(5, rnorm(4, mean=5))