r - Create sequence between certain values across two vectors -


i trying create sequences between values of different vectors not have same length.

imagine have 2 following vectors , b:

a<-c(1, 8, 14, 34, 46, 55) b<-c(3, 6, 12, 13, 18, 42, 49, 50, 57, 200) 

i generate third vector shows sequences between values of a , next highest value of b (here: 1:3 1,2,3; 8:12 8,9,10,11,12; 14:18 14,15,16,17,18 , on until 55:57 55,56,57).

using mapply did not yield desired results.

we can use findinterval subset 'b' based on value of 'a' , map corresponding sequence (:=) between elements of 'a' , subset elements of 'b'

map(`:`, a, b[findinterval(a, b) + 1]) #[[1]] #[1] 1 2 3  #[[2]] #[1]  8  9 10 11 12  #[[3]] #[1] 14 15 16 17 18  #[[4]] #[1] 34 35 36 37 38 39 40 41 42  #[[5]] #[1] 46 47 48 49  #[[6]] #[1] 55 56 57 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -