如何在 data.table 中进行否定/不匹配/反向搜索? [英] How do I do a negative / nomatch / inverse search in data.table?

查看:19
本文介绍了如何在 data.table 中进行否定/不匹配/反向搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想使用二进制搜索选择 data.table 中不包含键变量中特定值的所有行,会发生什么情况?顺便说一句,我想做的正确术语是什么?是不加入"吗?是负选择"吗?

What happens if I want to select all the rows in a data.table that do not contain a particular value in the key variable using binary search? By the way, what is the correct jargon for what I want to do? Is it "nojoin"? Is it "negative selection"?

DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
setkey(DT,x)

让我们对 x=="a" 但使用二分搜索的所有行进行肯定选择

Lets do a positive selection for all rows where x=="a" but using binary search

DT["a"]

这很漂亮,但我想要相反的.我想要所有不是a"的行,换句话说,x!="a"

That's beautiful but I want the opposite of that. I want all the rows that are not "a" in other words where x!="a"

DT[x!="a"]

那是矢量扫描.上述行有效,但使用矢量扫描.我想使用二进制.我期待以下工作,但唉......

That is a vector scanning. The above line works but is uses vector scanning. I want to use binary. I was expecting the following to work, but alas...

DT[!"a"]
DT[-"a"]

以上两个都不起作用,尝试使用 nomatch 让我无处可去.

The above two do not work and trying to play with nomatch got me nowhere.

推荐答案

成语是这样的:

DT[-DT["a", which=TRUE]]

   x y v
1: b 1 4
2: b 3 5
3: b 6 6
4: c 1 7
5: c 3 8
6: c 6 9

<小时>

灵感来自:


Inspiration from:

  • The mailing list posting Return Select/Join that does NOT match?
  • The previous question non-joins with data.tables
  • Matthew Dowle's answer to Porting set operations from R's data frames to data tables: How to identify duplicated rows?

更新.v1.8.3 中的新功能是非连接语法.Farrel 的第一个期望(! 而不是 -)已经实现:

Update. New in v1.8.3 is not-join syntax. Farrel's first expectation (! rather than -) has been implemented :

DT[-DT["a",which=TRUE,nomatch=0],...]   # old idiom
DT[!"a",...]                            # same result, now preferred.

查看 新闻 项目以获得更详细的信息和示例.

See the NEWS item for more detailed info and example.

这篇关于如何在 data.table 中进行否定/不匹配/反向搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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