R rep() Function: Repeating Elements of a Vector

R rep() is a generic function that replicates elements of vectors and lists for a specific number of times.

Diagram of rep() function

The best use case is when working with time-series analysis; you may want to repeat certain values to simulate specific conditions and obtain an accurate output, which is where the rep() function is helpful.

Syntax

rep(vec, times = 1, length.out = NA, each = 1)

Parameters

Name Description
vec It represents a vector or an object to be replicated.

It can be a character vector, a logical, or a list.

times It is an integer that suggests how many times to repeat an element of the length.

If it is a vector, it must match the length of the input x.

length.out It is a non-negative integer value that is the desired length of the output vector.
each It is an integer that specifies how many times each element of input x is repeated before moving to the next element.

Basic repetition of a vector

Let’s repeat an entire vector 3 times.

vec <- c(1, 2, 3)

repeated_vec <- rep(vec, times = 3)

print(repeated_vec)

# Output: [1] 1 2 3 1 2 3 1 2 3

Repeating each element

By passing “each” argument, it repeats each element of the vector before moving to the next element.

rep() function using each argument

# Define vector
rv <- c(1, 3, 11)

# Replicate the each vector four times
rep(rv, each = 4)

# Output: [1] 1 1 3 3 11 11

Prefer each argument over nested rep() calls for better performance.

Fixed-length output (length.out)

If you provide the length of the output vector, it will replicate the elements based on the input length. This is an ultimate argument that controls the length of the output vector, which determines how many times each element will be replicated.

vec <- c(19, 21)

rep(vec, length.out = 4)

# Output: [1] 19 21 19 21

Combining with times and each

rv <- c(19, 21)

rep(rv, each = 2, times = 2)

# Output: [1] 19  19  21  21  19  19  21  21

In the above code:

  1. each = 2: It means each element in a vector is repeated twice consecutively.
  2. times = 2: The whole sequence created by each parameter is repeated twice.

For large-scale repetition, use times vectorization instead of loops.

Repeating non-numeric vectors

It can also work well with character, logical, or other atomic vectors. Let’s use it with a logical vector.

logical_vector <- c(TRUE, FALSE)

repeated_logical_vec <- rep(logical_vector, times = 3)

print(repeated_logical_vec)

# Output: [1] TRUE FALSE TRUE FALSE TRUE FALSE

Usage with Lists

It not only works with vectors but also with lists. You can repeat the list of elements, preserving their structure.

main_list <- list(a = 1, b = 2)

repeated_list_elements <- rep(main_list, times = 3)

print(repeated_list_elements)

# Output:
# $a
# [1] 1

# $b
# [1] 2

# $a
# [1] 1

# $b
# [1] 2

# $a
# [1] 1

# $b
# [1] 2

Other rep() functions

rep.int()

The rep.int() function returns no attributes (except the class returns a factor).

rep.int() function in R

In the above figure, we are replicating elements 1, 2, 3, and 4 twice. That means the output vector contains eight elements: 1234 and 1234.

Syntax

rep.int(x, times)

Example

rep.int(1:4, 2)

# Output: [1] 1 2 3 4 1 2 3 4

rep_len()

The rep_len() function repeats elements of a vector, matrix, or array to achieve a specific output length.

rep_len() function in R

Syntax

rep_len(x, length.out)

Example

rep_len(19:21, 6)

# Output: [1] 19 20 21 19 20 21

That’s it.

Leave a Comment