R语言 使用df1中多列的值检查df2中的值

jm81lzqq  于 2022-12-27  发布在  其他
关注(0)|答案(2)|浏览(154)

我有这样的DF

df1 <- data.frame(ID=c(222717,321817,521917), Name=c("Bob","James","Eric"), Team=c("Badgers","Tigers","Possums"))
df2 <- data.frame(Badgers=c(222717,789423), Tigers=c(438283,978748), Possums=c(521917,251233))

我使用mapply在df2的相应Team列中检查df1的ID,如下所示

df1$result <- mapply(function(a,b) {b %in% df2[[a]]}, a=as.character(df1$Team), b=df1$ID)

我的问题是,现在有了新的df1,它们需要查找的ID列的数量可变,如下所示

newDf1 <- data.frame(ID1=c(222717,321817,521917), ID2=c(13998,978748,251233), Name=c("Bob","James","Eric"), Team=c("Badgers","Tigers","Possums"))

我曾尝试在mapply中使用grep执行此操作,但无法正常工作:

newDf1$result <- mapply(function(a,b) {b %in% df2[[a]]}, a=as.character(newDf1$Team), b=newDf1[grep("ID",names(newDf1))])

在这种情况下如何检查多个值呢?注意,正如我前面提到的,ID列的数量是可变的,因此,虽然下面的方法适用于我提供的示例newDf1(它只有两个ID列),但我需要适用于任意数量的ID列的方法。

newDf1$result <- mapply(function(a,b,c) {b %in% df2[[a]] | c %in% df2[[a]]}, a=as.character(newDf1$Team), b=newDf1$ID1, c=newDf1$ID2)
hjqgdpho

hjqgdpho1#

我们可以使用rowwise

library(dplyr)
newDf1 %>%
   rowwise %>%
   mutate(result = any(c_across(starts_with("ID")) %in% df2[[Team]])) %>% 
   ungroup

或者在base R

newDf1$result <- sapply(seq_len(nrow(newDf1)), \(i) 
  any(newDf1[i, grep("ID", names(newDf1))] %in% df2[[newDf1$Team[i]]]))
blpfk2vs

blpfk2vs2#

您可以将mapply()do.call()一起使用:

newDf1$result <- do.call(mapply, c(\(Team, ...) any(list(...) %in% df2[[Team]]),
  newDf1["Team"], newDf1[grep("ID", names(newDf1))]))

# Badgers  Tigers Possums
#    TRUE    TRUE    TRUE

相关问题