How to Repeat a Value N Times in R

· 1 min read · Updated March 15, 2026 · beginner
r rep repeat vectors

Repeating Values with rep()

# Repeat single value 5 times
rep(1, times = 5)
# [1] 1 1 1 1 1

# Repeat vector 3 times
rep(c("a", "b"), times = 3)
# [1] "a" "b" "a" "b" "a" "b"

# Repeat each element individually
rep(1:3, each = 2)
# [1] 1 1 2 2 3 3

# Force exact output length
rep(1:2, length.out = 7)
# [1] 1 2 1 2 1 2 1

# Create group labels
rep(c("control", "treatment"), each = 10)
# [1] "control" "control" ... "treatment" ...

See Also