Between vs inrange in data.table

In R data.table , when do I need to choose between %between% and %inrange% for subset operations? I read the man page for ?between , and I'm still scratching my head about the differences.

 library(data.table) X = data.table(a=1:5, b=6:10, c=c(5:1)) > X[b %between% c(7,9)] abc 1: 2 7 4 2: 3 8 3 3: 4 9 2 > X[b %inrange% c(7,9)] abc 1: 2 7 4 2: 3 8 3 3: 4 9 2 

They look the same to me. Can someone explain why both operations exist?

+3
r data.table
source share
1 answer
 > X abc 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)] abc 1: 3 8 3 2: 4 9 2 3: 5 10 1 > X[a %inrange% list(c, b)] abc 1: 1 6 5 2: 2 7 4 3: 3 8 3 4: 4 9 2 5: 5 10 1 

It seems that between looks at each line separately and checks if the value in is such that c <= a <= b for that line.

inrange searches for the smallest scalar value in c , say cmin and the largest scalar value in b , bmax , forming the range [cmin, bmax] , and then checks if a exists in this range [cmin, bmax] for each row in column a .

+1
source share

All Articles