r - Subsetting a vector into an another vector -


i have 2 input vectors shown below. need match ranges , need store them in separate vectors. works this: values in iv2 between first 2 values of iv1 stored in ov1, values in iv2 between second , third values of iv2 , on. note: values in input vectors in ascending order. thoughts please?

input:

iv1 <- c(100, 200, 300, 400, 435)  iv2 <- c(60, 120, 140, 160, 180, 230, 250, 255, 265, 270, 295, 340, 355, 401, 422, 424, 430) 

output:

ov1: 120, 140, 160, 180  ov2: 230, 250, 255, 265, 270, 295  ov3: 340, 355  ov4: 401, 422, 424, 430 

as @ronakshah suggested, efficient way in case may this:

split(iv2, cut(iv2, breaks = iv1,labels = paste0('ov',1:4))) 

output:

$ov1 [1] 120 140 160 180  $ov2 [1] 230 250 255 265 270 295  $ov3 [1] 340 355  $ov4 [1] 401 422 424 430 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -