Create Sequence of Repeated Values in R (Example) | Replicate N Times

 

In this R tutorial you’ll learn how to replicate a vector sequence many times.

The article will contain the following contents:

Let’s dive right into the example…

 

Creating Example Data

Consider the following example data:

x <- 1:5                # Create example data
x                       # Print example data
# 1 2 3 4 5

Have a look at the previously shown output of the RStudio console. It shows that our example data is a vector object containing numeric values with a range from 1 to 5.

 

Example: Replicating Values Multiple Times Using rep Function & each Argument

The code below illustrates how to duplicate our data elements several times using the each argument within the rep function. Have a look at the following R syntax:

rep(x, each = 5)        # Replicate data 5 times
# 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5

As you can see, we have duplicated every element of our sequence five times.

 

Video & Further Resources

Do you want to learn more about the replication of data? Then I can recommend to watch the following video of my YouTube channel. I’m illustrating the R programming codes of this tutorial in the video:

 

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.

 

Furthermore, you may want to have a look at the other tutorials of this website:

 

In summary: On this page you learned how to repeat a sequence multiple times in the R programming language. If you have further questions, please let me know in the comments below.

 

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

  • Hi Joachim ,

    Thanks for the tutorial ! I was wondering if its possible to replicate N times an excel file that contains 4 sheets for example.

    Valerie

    Reply
    • Hey Valerie,

      Thanks for the comment!

      For clarification: You want to create N different Excel files on your computer that contain exactly the same data?

      Regards,

      Joachim

      Reply

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.

Top