Search code examples
rvectorrowlapply

R generate number (Id) along sequence using two different vectors, lapply?


I have two vectors, which are basically starting and ending Row indices. I want to group them using this vectors. Example

a<-c(1,4,7,12)
b<-c(3,6,11,15)

my output vector should be

d <- c(1,1,1,2,2,2,3,3,3,3,3,4,4,4,4)

Solution

  • You can use rep to repeat value b-a times.

    rep(seq_along(a), (b - a) + 1)
    #[1] 1 1 1 2 2 2 3 3 3 3 3 4 4 4 4