R rep() is a generic function that replicates elements of vectors and lists for a specific number of times.
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.
rep(vec, times = 1, length.out = NA, each = 1)
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. |
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
By passing “each” argument, it repeats each element of the vector before moving to the next element.
# 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.
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
rv <- c(19, 21)
rep(rv, each = 2, times = 2)
# Output: [1] 19 19 21 21 19 19 21 21
In the above code:
For large-scale repetition, use times vectorization instead of loops.
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
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
The rep.int() function returns no attributes (except the class returns a factor).
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.
rep.int(x, times)
rep.int(1:4, 2)
# Output: [1] 1 2 3 4 1 2 3 4
The rep_len() function repeats elements of a vector, matrix, or array to achieve a specific output length.
rep_len(x, length.out)
rep_len(19:21, 6)
# Output: [1] 19 20 21 19 20 21
That’s it.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
The strsplit() function in R splits elements of a character vector into a list of…
The rnorm() method in R generates random numbers from a normal (Gaussian) distribution, which is…
The as.factor() function in R converts a vector object into a factor. Factors store unique…
R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…
The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…
The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…