# replicate Function in R (2 Examples)

This page demonstrates how to **perform a repeated evaluation of an expression using the replicate function** in R.

Table of contents:

Please note that the Rpdb package also provides a command called “replicate“. However, this tutorial discusses the application of the replicate function provided by Base R.

Let’s get started…

## Example 1: Basic Application of replicate() Function

Example 1 illustrates how to use the replicate function to repeat certain processes multiple times in the R programming language.

Have a look at the following application of the replicate function:

replicate(6, 1) # Apply replicate function # [1] 1 1 1 1 1 1 |

replicate(6, 1) # Apply replicate function # [1] 1 1 1 1 1 1

As you can see, the RStudio console has returned the value 1 six times after executing the previous R code. Within the replicate function, we have specified that we want to return the second part of the replicate function (i.e. the value 1) six times.

Easy peasy! However, the replicate function can be used in much more complex settings. So keep on reading!

## Example 2: Apply replicate() Function to User-defined Function

Example 2 demonstrates how to use the replicate function to repeat the application of a user-defined function multiple times.

First, we have to create our own function:

my_fun <- function() { # Create user-defined function runif(1) + 10 } |

my_fun <- function() { # Create user-defined function runif(1) + 10 }

Since our function involves random processes, we should also set a random seed for reproducibility:

set.seed(34976555) # Set random seed |

set.seed(34976555) # Set random seed

Next, we can apply the replicate function to execute our user-defined function multiple times:

replicate(3, my_fun()) # Apply replicate function # [1] 10.35626 10.66312 10.24438 |

replicate(3, my_fun()) # Apply replicate function # [1] 10.35626 10.66312 10.24438

As you can see, we have created a vector containing three output values.

Since the replicate function can be used to repeat certain processes several times, it is often used for simulation studies where random processes are involved.

## Video & Further Resources

Do you want to learn more about the application of the replicate function? Then I recommend having a look at the following video on my YouTube channel. I explain the R codes of this article in the video tutorial:

*The YouTube video will be added soon.*

Additionally, you may have a look at the other tutorials on my website. I have released numerous posts that are related to the application of the replicate function already:

- rep Function in R
- Create Sequence of Repeated Values in R
- Create Duplicate of Column in R
- List of R Commands
- R Programming Language

This tutorial has explained how to **apply the replicate function** in the R programming language. Please let me know in the comments, in case you have any further comments or questions.

### Statistics Globe Newsletter