R:根据另一列中的值提取一列中的唯一值匹配

如果 df2 中不存在,我想从 df1 打印字符串

df1 <- data.frame(One = c("userID1", "userID5", "userID9"))
df2 <- data.frame(Two = c("userID2", "userID4", "userID1", "userID7"))

如果 df2 中不存在,我想从 df1 打印字符串

Output <- data.frame(Two = c("userID5", "userID9"))

先感谢您。

回答

你可以使用dplyranti_join

library(dplyr)

df1 <- data.frame(One = c("userID1", "userID5", "userID9"))
df2 <- data.frame(Two = c("userID2", "userID4", "userID1", "userID7"))

df1 %>% 
  anti_join(df2, by = c("One" = "Two"))

返回

      One
1 userID5
2 userID9


回答

或者干脆

df1 <- data.frame(One = c("userID1", "userID5", "userID9"))
df2 <- data.frame(Two = c("userID2", "userID4", "userID1", "userID7"))

library(dplyr, warn.conflicts = F)
df1 %>%
  filter(!One %in% df2$Two)
#>       One
#> 1 userID5
#> 2 userID9

由reprex 包(v2.0.1)于 2021 年 8 月 19 日创建


以上是R:根据另一列中的值提取一列中的唯一值匹配的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>