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

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

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

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

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:


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


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:


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.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

2 Comments. Leave new

  • Thank you for posting very clear explanation on a range of topics. I need to replicate a function which gives a list of output. However, the replicate do not give anything and shows the output as:
    >Insp numeric,10 numeric,10 numeric,10 numeric,10 numeric,10
    >prop numeric,10 numeric,10 numeric,10 numeric,10 numeric,10
    Any feedback is really helpful. Thanks .


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.