Filter by ranges supplied by two vectors, without a join operation

后端 未结 2 1543
半阙折子戏
半阙折子戏 2021-01-12 11:23

I wish to do exactly this: Take dates from one dataframe and filter data in another dataframe - R

except without joining, as I am afraid that after I join my data th

2条回答
  •  情书的邮戳
    2021-01-12 11:45

    If you'd like to stick with dplyr it has similar functionality provided through the between function.

    # ranges I want to check between
    my_ranges <- list(c(2,2), c(4,5), c(6,7))
    
    tmp_df <- data.frame(a=1:10)
    tmp_df %>% 
      filter(apply(bind_rows(lapply(my_ranges, 
                                    FUN=function(x, a){
                                      data.frame(t(between(a, x[1], x[2])))
                                      }, a)
                             ), 2, any))
      a
    1 2
    2 4
    3 5
    4 6
    5 7
    

    Just be aware that the argument boundaries are included by default and that cannot be changed as with inrange

提交回复
热议问题