Making a vector filled with values

Problem

You want to create a vector with values already filled in.

Solution

  1. rep(1, 50)
  2. # [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  3. # [39] 1 1 1 1 1 1 1 1 1 1 1 1
  4. rep(F, 20)
  5. # [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  6. # [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  7. rep(1:5, 4)
  8. # 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
  9. rep(1:5, each=4)
  10. # 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
  11. # Use it on a factor
  12. rep(factor(LETTERS[1:3]), 5)
  13. # A B C A B C A B C A B C A B C
  14. # Levels: A B C