按第二个数据帧过滤数据帧

时间:2016-12-07 15:42:40

标签: r filter dplyr

我有两个数据帧。

selectedcustomersa是一个包含50个客户信息的数据框。 Fist列是名称(Group.1)。

selectedcustomersb是另一个数据框(相同结构),其中包含有关2000个客户的信息,其中包含来自selectedcustomersa的客户。

我希望selctedcustomersb没有来自selctedcustomersa的客户。

我试过了:

newselectedcustomersb<-filter(selectedcustomersb,  Group.1!=selectedcustomersa$Group.1) 

2 个答案:

答案 0 :(得分:3)

执行此操作的一种方法是在dplyr中使用anti_join,如下所示。它可以跨多个列等工作。

library(dplyr)
df1 <- data.frame(x = c('a', 'b', 'c', 'd'), y = 1:4)
df2 <- data.frame(x = c('c', 'd', 'e', 'f'), z = 1:4)
df <- anti_join(df2, df1)
df
  x z
1 e 3
2 f 4

答案 1 :(得分:2)

尝试:

newselectedcustomersb <- filter(selectedcustomersb, !(Group.1 %in% selectedcustomersa$Group.1))