在data.table中与范围之间 [英] between vs inrange in data.table

查看:63
本文介绍了在data.table中与范围之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R的 data.table 中,何时应该在%between%之间进行选择%inrange%用于子集操作?我已经阅读了之间的帮助页面?,但我仍在摸索差异。

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?

推荐答案

> 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 %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

似乎在之间单独查看每一行,并检查a中的值是否使该行的c< = a< = b。

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 寻找 c 中最小的标量值,例如 cmin b 中的最大标量值, bmax ,形成范围 [cmin ,bmax] ,然后检查 a 是否在此范围 [cmin,bmax] ,对于 a 列中的每一行。

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.

这篇关于在data.table中与范围之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆