简体   繁体   English

如何以更优雅的方式满足data.table的多条件?

[英]How to meet multi condition with data.table in more graceful way?

Here is the toy sample including 2 character variables. 这是包含2个字符变量的玩具样本。 I have another vector of two characters. 我有另外两个字符的向量。 By comparing one by one, I can get the result, but is there more graceful way of doing it? 通过逐一比较,我可以得到结果,但有更优雅的方式吗?

set.seed(100)
DT <- data.table(V1 = LETTERS[sample(1:5, 10, replace = T)], 
                 V2 = LETTERS[sample(3:7, 10, replace = T)])
V1V2 = c("B", "G")
DT[V1 %in% V1V2[1] & V2 %in% V1V2[2]]
#    V1 V2
# 1:  B  G

Since we can directly get the row elements by apply with DT[apply(DT[,.(V1, V2)], 1, print)] There shall be a way to describe a multi-condition express in i . 因为我们可以通过apply DT[apply(DT[,.(V1, V2)], 1, print)]直接获取行元素DT[apply(DT[,.(V1, V2)], 1, print)]应该有一种方法来描述i中的多条件表达。

I'm expecting something like: 我期待的是:

DT[.(V1, V2) %in% V1V2]

but this seems not to be working. 但这似乎不起作用。

Thanks for advice. 谢谢你的建议。

Another option is to use the join-capabilities of : 另一种选择是使用的join-capabilities:

setkey(DT, V1, V2)
DT[as.list(V1V2)]

or: 要么:

DT[as.list(V1V2), on = .(V1, V2)]

We could use as.list to compare column-wise every element in V1V2 我们可以使用as.listas.list列比较V1V2每个元素

DT  == as.list(V1V2)

#       V1    V2
# [1,]  TRUE FALSE
# [2,]  TRUE  TRUE
# [3,] FALSE FALSE
# [4,] FALSE FALSE
# [5,] FALSE FALSE
# [6,] FALSE FALSE
# [7,] FALSE FALSE
# [8,]  TRUE FALSE
# [9,] FALSE FALSE
#[10,] FALSE FALSE

This compares V1V2[1] with 1st column of DT and V1V2[2] with second column. 这将V1V2[1]与第一列DTV1V2[2]与第二列进行比较。

Now select rows where all elements are TRUE 现在选择所有元素都为TRUE

DT[rowSums(DT  == as.list(V1V2)) == ncol(DT), ]
#   V1 V2
#1:  B  G

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM