14

我有一个关于data.table“非连接”成语的问题,灵感来自迭代器的问题。这是一个例子:

library(data.table)

dt1 <- data.table(A1=letters[1:10], B1=sample(1:5,10, replace=TRUE))
dt2 <- data.table(A2=letters[c(1:5, 11:15)], B2=sample(1:5,10, replace=TRUE))

setkey(dt1, A1)
setkey(dt2, A2)

data.tables 看起来像这样

> dt1               > dt2
      A1 B1               A2 B2
 [1,]  a  1          [1,]  a  2
 [2,]  b  4          [2,]  b  5
 [3,]  c  2          [3,]  c  2
 [4,]  d  5          [4,]  d  1
 [5,]  e  1          [5,]  e  1
 [6,]  f  2          [6,]  k  5
 [7,]  g  3          [7,]  l  2
 [8,]  h  3          [8,]  m  4
 [9,]  i  2          [9,]  n  1
[10,]  j  4         [10,]  o  1

要查找 中的哪些行dt2具有相同的键dt1,请将which选项设置为TRUE

> dt1[dt2, which=TRUE]
[1]  1  2  3  4  5 NA NA NA NA NA

马修在这个答案中建议,“不加入”成语

dt1[-dt1[dt2, which=TRUE]]

dt1将那些具有未出现在中的索引的行作为子集dt2。在我的data.tablev1.7.1 机器上,我收到一个错误:

Error in `[.default`(x[[s]], irows): only 0's may be mixed with negative subscripts

相反,使用选项nomatch=0,“非加入”有效

> dt1[-dt1[dt2, which=TRUE, nomatch=0]]
     A1 B1
[1,]  f  2
[2,]  g  3
[3,]  h  3
[4,]  i  2
[5,]  j  4

这是预期的行为吗?

4

3 回答 3

18

v1.8.3 中的新功能:

A new "!" prefix on i signals 'not-join' (a.k.a. 'not-where'), #1384.
  DT[-DT["a", which=TRUE, nomatch=0]]   # old not-join idiom, still works
  DT[!"a"]                              # same result, now preferred.
  DT[!J(6),...]                         # !J == not-join
  DT[!2:3,...]                          # ! on all types of i
  DT[colA!=6L | colB!=23L,...]          # multiple vector scanning approach
  DT[!J(6L,23L)]                        # same result, faster binary search
'!' has been used rather than '-' :
  * to match the 'not-join' and 'not-where' nomenclature
  * with '-', DT[-0] would return DT rather than DT[0] and not be backwards
    compatibile. With '!', DT[!0] returns DT both before (since !0 is TRUE in
    base R) and after this new feature.
  * to leave DT[+...] and DT[-...] available for future use
于 2012-10-25T00:43:38.160 回答
5

据我所知,这是基础 R 的一部分。

# This works
(1:4)[c(-2,-3)]

# But this gives you the same error you described above
(1:4)[c(-2, -3, NA)]
# Error in (1:4)[c(-2, -3, NA)] : 
#   only 0's may be mixed with negative subscripts

文本错误消息表明这预期行为。

这是我对为什么这是预期行为的最佳猜测 :

从他们在别处对待NA' 的方式来看(例如,通常默认为na.rm=FALSE),R 的设计者似乎将NA' 视为携带重要信息,并且不愿意在没有明确指示的情况下放弃它。(幸运的是,设置nomatch=0为您提供了一种清晰的方式来传递该指令!)

在这种情况下,设计者的偏好可能解释了为什么NA' 被接受为正索引,但不被接受为负索引:

# Positive indexing: works, because the return value retains info about NA's
(1:4)[c(2,3,NA)]

# Negative indexing: doesn't work, because it can't easily retain such info
(1:4)[c(-2,-3,NA)]
于 2011-10-27T18:49:18.703 回答
2

data.table 1.7.3 版中的新功能:

新选项datatable.nomatch允许将 nomatch 的默认值从 NA 更改为 0,...

于 2011-11-15T23:44:50.910 回答