39

I have quick question regarding sequence and each:

vect1 <- c(4, 5, 10, 3, 1)

I want replicate with this vector as each such that first number is replicated 4, second 5, third 10, fourth 3, and fifth equal 1.

rep(1:5, each = vect1) 
 [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
Warning message:
In rep(1:5, each = vect1) : first element used of 'each' argument

rep(1:5, each = c(4, 5, 10, 3, 1)) 

    [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
    Warning message:
    In rep(1:5, each = c(4, 5, 10, 3, 1)) :
      first element used of 'each' argument

I know this is misuse of each.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
jon
  • 11,186
  • 19
  • 80
  • 132

1 Answers1

27
rep(1:5, vect1)

If you have questions about how to work functions in R, try

?function

where "function" is whatever function you want to know about. From ?rep you would have read:

'times' A integer vector giving the (non-negative) number of times to repeat each element if of length length(x), or to repeat the whole vector if of length 1. Negative or NA values are an error.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Rguy
  • 1,622
  • 1
  • 15
  • 20