rep()

R
rep
Published

2017-06-21

rep(x, times = 1, length.out = NA, each = 1) is pretty useful for simulating data. Here are some common recipes:

rep(1, 10)
 [1] 1 1 1 1 1 1 1 1 1 1
rep(1:5, 2)
 [1] 1 2 3 4 5 1 2 3 4 5
rep(1:5, times=2)
 [1] 1 2 3 4 5 1 2 3 4 5
rep(1:5, times=2.9)
 [1] 1 2 3 4 5 1 2 3 4 5
rep(1:5, each=2)
 [1] 1 1 2 2 3 3 4 4 5 5
rep(1:5, 1, NA, 2)
 [1] 1 1 2 2 3 3 4 4 5 5
rep(c(0, 3, 6), times=2)
[1] 0 3 6 0 3 6
rep(c("a", "b", "c"), times=2)
[1] "a" "b" "c" "a" "b" "c"
rep(c("a", "b", "c"), each=2)
[1] "a" "a" "b" "b" "c" "c"
rep(c("a", "b"), each=2, times=3)
 [1] "a" "a" "b" "b" "a" "a" "b" "b" "a" "a" "b" "b"
rep(1:5, length.out=12)
 [1] 1 2 3 4 5 1 2 3 4 5 1 2
rep_len(1:5, 12)
 [1] 1 2 3 4 5 1 2 3 4 5 1 2
rep(1:5, length.out=12, times=500)
 [1] 1 2 3 4 5 1 2 3 4 5 1 2
rep(seq(0, 10, by=5), 3)
[1]  0  5 10  0  5 10  0  5 10