Replicate elements of vector in R programming – rep() Method
In the R programming language, A very useful function for creating a vector by repeating a given numbervector with the specified number of times is the rep().
The general structure of rep() : rep(v1,n1).
Here, v1 is repeated n1 times.
R – replicate elements of vector
The forms of rep() functions :
- rep(v1, times=)
- rep(v1, each=)
- rep(v1, length=)
Example 1:
Output:
[1] 0 0 0 0 0
Example 2:
rep(v1, times=)
Output:
[1] 1 2 3 1 2 3 1 2 3
Example 3:
rep(v1, each=)
Output:
[1] 1 2 3 1 2 3 1 2 3
Example 4:
rep(v1, length=)
Output:
1 2 3 1 2
Example 5:
Output:
1 1 2 3 3 3
Last Updated :
21 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...