我想查找并测试一组(“ set A”)中的值是否出现在 组B 或组C中。我试图使用%in%
运算符用于此目的,但无法弄清楚如何将其与OR结合使用。
下面是一个可重现的示例,但是我想要得到的要点是:
set_a %in% (set_b | set_c)
我想知道 set_a 中的哪些值存在于 set_b 或 set_c 或两者中。
#Step 1 :: Creating the data
set_a <- unlist(strsplit("Eden Kendall Cali Ari Madden Leo Stacy Emmett Marco Bridger Alissa Elijah Bryant Pierre Sydney Luis", split=" "))
set_b <- as.data.table(unlist(strsplit("Kathy Ryan Brice Rowan Nina Abram Miles Kristina Gabriel Madden Jasper Emmett Marco Bridger Alissa Elijah Bryant Pierre Sydney Luis", split=" ")))
set_c <- as.data.table(unlist(strsplit("Leo Stacy Emmett Marco Moriah Nola Jorden Dalia Kenna Laney Dillon Trystan Elijah Bryant Pierr", split=" ")))
NamesList <- list(set_b, set_c) #set_b and set_c will now become neighboring data.table dataframes in one list.
> NamesList
[[1]]
V1
1: Kathy
2: Ryan
3: Brice
4: Rowan
5: Nina
6: Abram
7: Miles
8: Kristina
9: Gabriel
10: Madden
11: Jasper
12: Emmett
13: Marco
14: Bridger
15: Alissa
16: Elijah
17: Bryant
18: Pierre
19: Sydney
20: Luis
[[2]]
V1
1: Leo
2: Stacy
3: Emmett
4: Marco
5: Moriah
6: Nola
7: Jorden
8: Dalia
9: Kenna
10: Laney
11: Dillon
12: Trystan
13: Elijah
14: Bryant
15: Pierr
#Step 2 :: Checking which values from set_a appear in either set_b or set_c
matches <- set_a %in% (set_b | set_c)
#doesn't work!
有什么想法吗?顺便说一句,使用data.table格式对我来说很重要。
答案 0 :(得分:1)
您可以单独尝试条件
set_a %in% set_b | set_a %in% set_c
或使用union
或unique
set_a %in% union(set_b, set_c)
set_a %in% unique(c(set_b, set_c))
答案 1 :(得分:0)
我们可以使用
Reduce(`|`, lapply(list(set_b, set_c), `%in%`, set_a))