36

I am trying to take an existing vector and repeat each element of it six times. I feel like this should be easy using rep() but I keep hitting the wall. Basically I would like to take this vector:

1027 1028 1030 1032 1037

And turn it into this:

1027 1027 1027 1027 1027 1027 1028 1028 1028 1028 1028 1028 ...
zx8754
  • 52,746
  • 12
  • 114
  • 209
Stedy
  • 7,359
  • 14
  • 57
  • 77
  • 17
    This question is easily solved by using the help function. type ?rep at the command line for this one. No bad intended, learning to use the help in R is really going to save you a lot of time. – Joris Meys Sep 08 '10 at 23:04

1 Answers1

58

Use each argument:

rep(c(1027, 1028, 1030, 1032, 1037), each = 6)
#  [1] 1027 1027 1027 1027 1027 1027
#  [7] 1028 1028 1028 1028 1028 1028
# [13] 1030 1030 1030 1030 1030 1030
# [19] 1032 1032 1032 1032 1032 1032
# [25] 1037 1037 1037 1037 1037 1037

times argument:

rep(c(1027, 1028, 1030, 1032, 1037), times = 6)
#  [1] 1027 1028 1030 1032 1037
#  [6] 1027 1028 1030 1032 1037
# [11] 1027 1028 1030 1032 1037
# [16] 1027 1028 1030 1032 1037
# [21] 1027 1028 1030 1032 1037
# [26] 1027 1028 1030 1032 1037
zx8754
  • 52,746
  • 12
  • 114
  • 209
mbq
  • 18,510
  • 6
  • 49
  • 72