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.
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:
- Repeat Rows of Data Frame N Times
- Unique Rows of Data Frame Based On Selected Columns
- Remove Duplicated Rows from Data Frame
- rep Function in R
- The R Programming Language
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.
Statistics Globe Newsletter