R滚动%在%之间

时间:2015-07-11 21:59:52

标签: r data.table

寻找滚动%between%功能。
如果在调用之间的边缘上没有完全匹配的值,那么它将采用下一个值(如果可能的话) 例如,我们调用函数%between1%

library(data.table)
dt <- data.table(date = as.Date(c("2015-06-28","2015-06-30","2015-07-04","2015-07-05","2015-07-05")),
                 val = letters[1:5])
dt[date %between% as.Date(c("2015-07-02","2015-07-05"))]
#          date val
# 1: 2015-07-04   c
# 2: 2015-07-05   d
# 2: 2015-07-05   e
dt[date %between% as.Date(c("2015-07-02","2015-07-05")), roll=TRUE] # no effect
#          date val
# 1: 2015-07-04   c
# 2: 2015-07-05   d
# 2: 2015-07-05   e


dt[date %between1% as.Date(c("2015-07-02","2015-07-05"))]
### expected output:
#          date val
# 1: 2015-06-30   b
# 1: 2015-07-04   c
# 2: 2015-07-05   d
# 2: 2015-07-05   e

寻找%between1%的高性能解决方案,应该以某种方式使用二进制搜索和滚动。

0 个答案:

没有答案