HOWTO · R

The replicate() Function in R

This tutorial introduces the uses of the replicate() function in R.

On this page

R’s replicate() evaluates an expression repeatedly and collects each result. It is useful when the same calculation, random draw, or simulation needs to be run several times. The expression’s result and the simplify argument determine whether the collected values form a vector, matrix, array, or list.

What replicate() Repeats

The first argument, n, gives the number of evaluations. The second argument, expr, is evaluated once per repetition. For example, this returns five copies of the scalar value 1:

Run this example:

result <- replicate(5, 1)
print(result)

The returned vector contains one value for each evaluation. If expr uses randomness, each evaluation can produce a different value; replicate() collects those results without making them identical.

Syntax and the simplify Argument

The general form is replicate(n, expr, simplify = "array"). Here, n is the repetition count, expr is the expression to evaluate, and simplify controls how R simplifies the collected results. The default, "array", allows results to be simplified according to their shape: scalar results can form a vector, equal-length vectors can form a matrix, and higher-dimensional results can form an array. Use simplify = FALSE to retain a list, for example when each repetition returns a result that should remain a separate object.

Run this example:

values <- replicate(3, c("red", "blue"), simplify = FALSE)
print(typeof(values))
print(length(values))
[1] "list"
[1] 3

typeof() reports the underlying type, and length() counts the list elements.

Repeat a Scalar or Vector

A scalar expression produces one result per repetition. If the expression returns a vector of equal length each time, replicate() places each vector in a column of a matrix. This makes the rows correspond to positions within the vector and the columns correspond to repetitions.

Run this example:

result <- replicate(3, seq(1, 4))
print(result)

Use the same pattern for a calculation that returns a fixed-length vector. If result lengths vary, the values cannot be arranged as a regular matrix; set simplify = FALSE to keep each result in a list element.

Use replicate() With a for() Loop

A for() loop can build a value that becomes the result of each repetition. Initialize and fill a vector inside the expression, then make that vector the expression’s final value:

Run this example:

result <- replicate(2, {
  values <- numeric(4)
  for (i in seq_len(4)) {
    values[i] <- i * 2
  }
  values
})
print(result)

The final expression is values, so each evaluation returns the completed vector and the simplified result has one column per repetition. A for() loop itself returns NULL; printing values inside a loop does not make those printed values its return value. If the repeated expression consists only of such a loop, each repetition returns NULL. Store the values and return the storage object when you need them in the result.

Repeat Random Simulations

Random expressions are evaluated afresh on each repetition. For instance, rnorm(4) draws four values from a standard normal distribution. The resulting matrix has one column per draw of four values; the sampled numbers vary between runs, so they should not be treated as fixed output.

Run this example:

set.seed(42)
draws <- replicate(3, rnorm(4))
print(dim(draws))

A simulation function can also be repeated. Define it to return the statistic or measurement of interest, then pass a call to that function as expr. For example, a function that draws one value with runif(1) can be evaluated many times to study the distribution of those simulated values. Choose and record a seed when you need a reproducible random sequence; without a fixed seed, the values will generally differ between runs.

Bootstrap Resampling With replicate()

Bootstrap resampling approximates uncertainty by drawing samples with replacement from observed data. In each repetition, sample() creates one resampled dataset. When each sample has the same size as the original, the simplified output is a matrix whose columns are bootstrap replicates.

Run this example:

original_data <- c(2, 4, 6, 8, 10)
set.seed(42)
resamples <- replicate(5, sample(original_data, replace = TRUE))
print(dim(resamples))

Each column represents one resampled dataset, and values may appear more than once because sampling is with replacement. To bootstrap a statistic rather than retain every sampled value, calculate the statistic inside the repeated expression, such as mean(sample(original_data, replace = TRUE)). That produces one statistic per repetition, which is typically collected in a vector.