NDA
NDA

Reputation: 13

How do I extract the range of values for each row between two vectors?

I am trying to get the range of values for each row between two vectors in a single output vector. So, if the data I'm working with are:

v1<-c(1,2,3)
v2<-c(4,5,6)

I'd like the output to be:

1 2 3 4 2 3 4 5 3 4 5 6

which is basically:

c(1:4, 2:5, 3:6)

Thanks!

Upvotes: 1

Views: 43

Answers (1)

akrun
akrun

Reputation: 887118

We can use Map to get the sequence (:) of corresponding elements of both vectors and then unlist the list output

unlist(Map(`:`, v1, v2))
#[1] 1 2 3 4 2 3 4 5 3 4 5 6

Or using a for loop

out <- c()
for(i in seq_along(v1)) out <- c(out, v1[i]:v2[i])

Upvotes: 1

Related Questions