Reputation:
In R's data.table
, when should one choose between %between%
and %inrange%
for subsetting operations? I've read the help page for ?between
and I'm still scratching my head as to the differences.
library(data.table)
X = data.table(a=1:5, b=6:10, c=c(5:1))
> X[b %between% c(7,9)]
a b c
1: 2 7 4
2: 3 8 3
3: 4 9 2
> X[b %inrange% c(7,9)]
a b c
1: 2 7 4
2: 3 8 3
3: 4 9 2
They look the same to me. Could someone please explain why there exist both operations?
Upvotes: 8
Views: 5206
Reputation:
> X
a b c
1: 1 6 5
2: 2 7 4
3: 3 8 3
4: 4 9 2
5: 5 10 1
Using the example in the comments:
> X[a %between% list(c, b)]
a b c
1: 3 8 3
2: 4 9 2
3: 5 10 1
> 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
It seems between
looks at each row individually and checks to see if the value in a is such that c <= a <= b for that row.
inrange
looks for the smallest scalar value in c
, say cmin
and the largest scalar value in b
, bmax
, forming a range [cmin, bmax]
, and then checks to see if a
lies in this range [cmin, bmax]
, for each row in the a
column.
Upvotes: 8