如何将 %in% 与 OR 运算符结合使用?

How to combine the use of %in% with OR operator?

我想查找并测试一组 ("set A") 中的值是否出现在 either set B or[=28= 中] 设置 C。我试图为此目的使用 %in% 运算符,但无法弄清楚如何将它与 OR 结合使用。

底部是一个可重现的例子,但我想要得到的要点是这样的:

set_a %in% (set_b | set_c) 

我想知道 set_a 中的哪些值存在于 set_bset_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 格式对我来说很重要。

您可以单独尝试条件

set_a %in% set_b | set_a %in% set_c

或使用unionunique

set_a %in% union(set_b, set_c)

set_a %in% unique(c(set_b, set_c))

我们可以使用

Reduce(`|`, lapply(list(set_b, set_c), `%in%`, set_a))