rep(x, times = 1, length.out = NA, each = 1) is pretty useful for simulating data. Here are some common recipes:
- Repeat a single number (1) a number of times (10)
- Repeat a series of numbers (1:5) a number of times (2)
- If
times is not an integer, it is truncated (not rounded).
- Repeat a series of numbers (1:5) a number of times each (2)
- Repeat a list of numbers (0, 3, 6) a number of times (2)
- Repeat a list of strings (“a”, “b”, “c”) a number of times (2)
rep(c("a", "b", "c"), times=2)
[1] "a" "b" "c" "a" "b" "c"
- Repeat a list of strings (“a”, “b”, “c”) a number of times each (2)
rep(c("a", "b", "c"), each=2)
[1] "a" "a" "b" "b" "c" "c"
- Repeat a list of strings (“a”, “b”) a number of times each (2) a number of times (3)
rep(c("a", "b"), each=2, times=3)
[1] "a" "a" "b" "b" "a" "a" "b" "b" "a" "a" "b" "b"
- Repeat a series of numbers (1:5) until you have a specific total (12)
[1] 1 2 3 4 5 1 2 3 4 5 1 2
[1] 1 2 3 4 5 1 2 3 4 5 1 2
length.out overrides times
rep(1:5, length.out=12, times=500)
[1] 1 2 3 4 5 1 2 3 4 5 1 2
- Repeat a sequence of numbers (0:10 by 5s) a number of times (3)