# rep()

`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)
``rep(1, 10)``
``##   1 1 1 1 1 1 1 1 1 1``
• Repeat a series of numbers (1:5) a number of times (2)
``rep(1:5, 2)``
``##   1 2 3 4 5 1 2 3 4 5``
``rep(1:5, times=2)``
``##   1 2 3 4 5 1 2 3 4 5``
• If `times` is not an integer, it is truncated (not rounded).
``rep(1:5, times=2.9)``
``##   1 2 3 4 5 1 2 3 4 5``
• Repeat a series of numbers (1:5) a number of times each (2)
``rep(1:5, each=2)``
``##   1 1 2 2 3 3 4 4 5 5``
``rep(1:5, 1, NA, 2)``
``##   1 1 2 2 3 3 4 4 5 5``
• Repeat a list of numbers (0, 3, 6) a number of times (2)
``rep(c(0, 3, 6), times=2)``
``##  0 3 6 0 3 6``
• Repeat a list of strings (“a”, “b”, “c”) a number of times (2)
``rep(c("a", "b", "c"), times=2)``
``##  "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)``
``##  "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)``
``##   "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)
``rep(1:5, length.out=12)``
``##   1 2 3 4 5 1 2 3 4 5 1 2``
``rep_len(1:5, 12)``
``##   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 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)
``rep(seq(0, 10, by=5), 3)``
``##   0  5 10  0  5 10  0  5 10`` ##### Lisa DeBruine
###### Professor of Psychology

Lisa DeBruine is a professor of psychology at the University of Glasgow. Her substantive research is on the social perception of faces and kinship. Her meta-science interests include team science (especially the Psychological Science Accelerator), open documentation, data simulation, web-based tools for data collection and stimulus generation, and teaching computational reproducibility.