【问题标题】:Filter Dataframe by second dataframe [duplicate]按第二个数据框过滤数据框[重复]
【发布时间】:2017-04-22 15:22:27
【问题描述】:

我有两个数据框。

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

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

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

我试过了:

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

【问题讨论】:

  • 你可以使用setdiff。类似newselectedcustomersb &lt;- selectedcustomersb[selectedcustomersb$Group.1 %in% setdiff(selectedcustomersb$Group.1, selectedcustomersa$Group.1),]

标签: r filter dplyr


【解决方案1】:

试试:

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

【讨论】:

    【解决方案2】:

    一种方法是在 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
    

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2018-03-08
      • 2018-10-22
      • 2018-10-24
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 2017-06-28
      相关资源
      最近更新 更多