rep in R: How to Use rep() Function in R

The rep() is an iteration function in R. The term iteration means repetition.  An iteration is a core aspect of R. The regular loops are like for loop, while loop is costly in time and memory management.

The rep() function is a good vectorized alternative method whose goal is to achieve iteration. Vectorized methods operate on all the vector elements concurrently, and vectorized computations always get faster results.

rep in R

The rep() is a built-in generic R function that replicates the values in the provided vector.  The rep() method takes a vector as an argument and returns the replicated values. Thus, the rep() is a vectorized looping function whose only goal is to achieve iteration without costing time and memory.

The rep() function in R has two faster-simplified versions.

  1. rep.int()
  2. rep_len()

Syntax

rep(rv, …)

Parameters

The rep() function takes a maximum of two arguments.

The rv is a vector (of any mode, including a list) or a factor.

The (…) are further arguments to be passed to or from other methods. For example, it can be one of the following.

  1. times: It is an integer-valued vector giving the (non-negative) number of times to repeat each item if of length. For example, length(x), or to repeat the whole vector if of length 1. 
  2. length.out: It is a non-negative integer—the desired length of the output vector. 
  3. each: It is a non-negative integer. Each item of x is repeated each time.

Example

rep(11, 4)

Output

[1] 11 11 11 11

In this example, we are repeating vector 11 four times.

To repeat NA values more than one time, use the rep() function.

rep(NA, 5)

Output

[1] NA NA NA NA NA

Repeat counting from number to number in R

Repeat the counting numbers from 1 to 4 three times.

rep(1:4, 3)

Output

 [1] 1 2 3 4 1 2 3 4 1 2 3 4

You can see that 1 through 4 is repeated four times.

Repeat vector with an incomplete cycle in R

To repeat a vector with an incomplete cycle, use the length.out argument in the rep() function.

rep(1:4, 3, length.out=9)

Output

[1] 1 2 3 4 1 2 3 4 1

By providing length.out argument, you can restrict the length of the output. So, for example, you can see that the cycle is incomplete because output should consist of 12 integers, but instead, it contains 9 integers.

Passing each argument to the rep() function

Each parameter is a non-negative integer. Thus, each element of x is repeated each time.

rep(1:4, each=2)

Output

[1] 1 1 2 2 3 3 4 4

You can see that each element from 1 to 4 is repeated 2 times.

Automated length repetition

A vector can expand to a biased panel by replacing the length parameter with a vector that defines the number of times each item in the vector will repeat.

rep(1:4, 1:4)

Output

 [1] 1 2 2 3 3 3 4 4 4 4

Here, you can see that 1 appears 1 time, 2 two times, 3 three times, and 4 four times.

Using the rep() function to replicate a list

You can use the rep() function to replicate a list in R.

data <- list(netflix = 1:4)
rep(data, 4)

Output

$netflix
[1] 1 2 3 4

$netflix
[1] 1 2 3 4

$netflix
[1] 1 2 3 4

$netflix
[1] 1 2 3 4

In the above example, the Netflix list of 1 to 4 has been replicated four times.

Using the rep() function to replicate a factor

The rep() function in R can replicate the factor.

result <- factor(LETTERS[1:4])
rep(result, 4)

Output

[1] A B C D A B C D A B C D A B C D
Levels: A B C D

R rep.int()

The rep.int() function returns no attributes (except the class returns a factor). The rep.int() is a simple case provided as a separate function, partly for compatibility and speed. The syntax is rep.int(x, times).

rep.int(1:4, 2)

Output

[1] 1 2 3 4 1 2 3 4

R rep_len()

The syntax is rep_len(x, length.out).

rep_len(1:3, 10)

Output

 [1] 1 2 3 1 2 3 1 2 3 1

In this example, 1 to 3 is repeated until the length.out is reached.

Conclusion

Traditional iterations are helpful in some cases, but the major drawback is that it consumes time and memory. On the other hand, the rep() function in R is excellent for replicating the values of a list or vector, and it is also time and memory-effective.

Leave a Comment