Ale*_*lex 7 r subset dplyr data.table
我希望这样做:从一个数据帧中获取日期并过滤另一个数据帧中的数据 - R.
除非没有加入,因为我担心在加入我的数据后,结果将太大而无法放入内存,在过滤器之前.
以下是示例数据:
tmp_df <- data.frame(a = 1:10)
Run Code Online (Sandbox Code Playgroud)
我希望做一个看起来像这样的操作:
lower_bound <- c(2, 4)
upper_bound <- c(2, 5)
tmp_df %>%
filter(a >= lower_bound & a <= upper_bound) # does not work as <= is vectorised inappropriately
Run Code Online (Sandbox Code Playgroud)
我期望的结果是:
> tmp_df[(tmp_df$a <= 2 & tmp_df$a >= 2) | (tmp_df$a <= 5 & tmp_df$a >= 4), , drop = F]
# one way to get indices to subset data frame, impractical for a long range vector
a
2 2
4 4
5 5
Run Code Online (Sandbox Code Playgroud)
我的内存需求问题(关于连接解决方案链接)是什么时候tmp_df有更多的行,lower_bound而upper_bound矢量有更多的条目.甲dplyr溶液或溶液,可以是管的一部分是优选的.
也许你可以借inrange由函数data.table,这
检查x中的每个值是否介于lower,upper中提供的任何间隔之间.
用法:
inrange(x,lower,upper,incbounds = TRUE)
library(dplyr); library(data.table)
tmp_df %>% filter(inrange(a, c(2,4), c(2,5)))
# a
#1 2
#2 4
#3 5
Run Code Online (Sandbox Code Playgroud)