What is the replicate() function in R?

Overview

The replicate() function in R creates a repeated expression or function for a specified number of times.

Syntax

The replicate() function takes the syntax below:

replicate(x, ...)
The syntax for the replicate() function in R

Parameter value

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.

Example 1

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 R
replicate(5, seq(1,5,1))

Example 2

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() function
replicate(5, rnorm(4, mean=5))

Free Resources