data.table中 between 与 inrange的区别
2019-04-27 本文已影响3人
热衷组培的二货潜
问题来源于一片博客data.table 与 dplyr 的区别
查阅到 between vs inrange in data.table
- 官方说明书
-
betweenis equivalent tox >= lower & x <= upperwhenincbounds=TRUE, orx > lower & y < upperwhenFALSE, It is set toTRUEbydefaultfor infix notations. With a caveat thatNAin lower or upper are taken as a missing bound and returnTRUEnotNA. -
inrangechecks whether each value inxis in betweenany of the intervals provided in lower,upper.
between(x, lower, upper, incbounds=TRUE)
x %between% y
inrange(x, lower, upper, incbounds=TRUE)
x %inrange% y
> library(data.table)
> X = data.table(a=1:5, b=6:10, c=c(5:1))
> X
a b c
1: 1 6 5
2: 2 7 4
3: 3 8 3
4: 4 9 2
5: 5 10 1
> X[a %between% list(c, b)]
a b c
1: 3 8 3
2: 4 9 2
3: 5 10 1
> X[a %between% list(b, c)]
Empty data.table (0 rows) of 3 cols: a,b,c
> X[a %inrange% list(c, b)]
a b c
1: 1 6 5
2: 2 7 4
3: 3 8 3
4: 4 9 2
5: 5 10 1
> X[a %inrange% list(b, c)]
Empty data.table (0 rows) of 3 cols: a,b,c
- 从上面可以看出
X[a %between% list(c, b)]中%between%表示的是Cn ≤ An ≤ Bn的行 - 而
X[a %inrange% list(c, b)]表示是:假设在列c中的最小值为Cmin,列b中的最大值为Bmax,那么结果就是符合Cmin ≤ a ≤ Bmax的行,其实不难理解,我们可以看到当list(c, b)变为list(b, c)时候是没有结果输出的。