10

How to assign more than one value for "each" argument in "rep" function in R? A trivial example, where each value in a vector is 3-times repeated in a row:

a <- seq(2,6,2)
rep (a,each = 3)

However, if I add more than one value in "each" argument in order to change the number of repetition of each value, it doesn't work properly:

rep (a, each = c(2,4,7))

How to solve it? Thank you in advance.

zx8754
  • 52,746
  • 12
  • 114
  • 209
user36478
  • 346
  • 6
  • 14

1 Answers1

18

Depending on what you think the output should be, I'm guessing you want the times= parameter:

rep (a, times = c(2, 4, 7))
# [1] 2 2 4 4 4 4 6 6 6 6 6 6 6

See ?rep for the difference

zx8754
  • 52,746
  • 12
  • 114
  • 209
MrFlick
  • 195,160
  • 17
  • 277
  • 295