- 官方说明书
-
between
is equivalent to x >= lower & x <= upper
when incbounds=TRUE
, or x > lower & y < upper
when FALSE
, It is set to TRUE
by default
for infix notations. With a caveat that NA
in lower or upper are taken as a missing bound and return TRUE
not NA
.
-
inrange
checks whether each value in x
is in between any 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)
时候是没有结果输出的。
网友评论