VOOZH about

URL: https://www.geeksforgeeks.org/r-language/replicate-elements-of-vector-in-r-programming-rep-method/

⇱ Replicate elements of vector in R programming - rep() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Replicate elements of vector in R programming - rep() Method

Last Updated : 28 Apr, 2025

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
Comment
Article Tags:
Article Tags:

Explore