[英]lookup using two columns with unique rows in R data.table
I am wondering if it is possible to use two columns to do a lookup in R data.table. 我想知道是否可以使用两列在R data.table中进行查找。 Here is a little experiment that failed:
这是一个失败的小实验:
x <- data.table(A = c("a", "a", "b", "b", "c", "c"),
D = c( 1, 2, 1, 2, 4, 5))
DT <- data.table(A = c("a", "a", "b", "b"),
D = c( 1, 2, 1, 2))
setkey(DT, A, D)
DT[J(x$A, x$D), ] # Same as below, thanks to ilir, I thought it returns an error previously
DT[J(x$A, x$D), , allow.cartesian=TRUE]
# Return:
# A D
# 1: a 1
# 2: a 2
# 3: b 1
# 4: b 2
# 5: c 4 # <- ideally (NA NA) or (c NA)
# 6: c 5 # <- ideally (NA NA) or (c NA)
In this experiment, rows in DT are unique, however, both columns have duplicated entries. 在此实验中,DT中的行是唯一的,但是,两列都有重复的条目。 When calling DT[J(x$A, x$D), ], what I want to do is to lookup table DT, thus I would expect the result only has entries in DT, however, this is clearly not the case.
当调用DT [J(x $ A,x $ D),]时,我要做的是查找表DT,因此我希望结果仅在DT中具有条目,但是,显然不是这样。
Is there an effective way to do a lookup with two columns as keys? 有没有一种有效的方法以两列为键进行查找?
Thanks to ilir, the following code works: 感谢ilir,以下代码可以正常工作:
x <- data.table(A = c("a", "a", "b", "b", "c", "c"),
D = c( 1, 2, 1, 2, 4, 5))
DT <- data.table(A = c("a", "a", "b", "b"),
D = c( 1, 2, 1, 2))
DT[, aux := 1L]
setkey(DT, A, D)
DT[J(x$A, x$D), ]
inx <- !is.na(DT[J(x$A, x$D), ]$aux)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.