【问题标题】:Remove Rows From Data Frame where a Row match from a list从列表中的行匹配的数据框中删除行
【发布时间】:2019-02-14 19:40:26
【问题描述】:

从数据框中删除列表中值匹配的行 > 我的帐户列表如下:

ANrule4 <- 
Group_Account
2911
2944
2949
1415
1695
1761
1912
2570

但我想删除以下列表中列出的任何帐户:

2911
2946
2945
2944
2949

我正在使用以下代码:

ANrules4ex <- ANrule4%>%
              filter(!(Group_Account==2946 | Group_Account==2945 | Group_Account==2944 | Group_Account==2942 | Group_Account==2941 |   Group_Account==2912 | Group_Account==2911 | Group_Account==2910 ))

这工作正常,但实际上我的列表非常长且动态,我想将排除列表存储在一个列表中,并希望合并这两个列表,将所有帐户都列在排除列表中,但不知道该怎么做。谁能帮我解决这个问题

【问题讨论】:

    标签: r filter merge conditional


    【解决方案1】:

    试试这个: df 是您的data.framedata 是您要比较的向量。

    df[!(df$Group_Account %in% data),]
    

    【讨论】:

    • sorry Saurabh 但它对我不起作用,我尝试了以下方式:fm1
    • @Ashish:它正在处理您的示例数据。请提供可重现的示例。
    【解决方案2】:

    您可以使用dplyr 包中的anti_join

    ANrule4 <-
      data.frame(Group_Account = c(2911, 2944, 2949, 1415, 1695, 1761, 1912, 2570))
    
    listremove <-
      data.frame(Group_Account = c(2911, 2946, 2945, 2944, 2949))
    
    ANrule4 %>% anti_join(listremove, by = "Group_Account")
    
      Group_Account
    1          1415
    2          1695
    3          1761
    4          1912
    5          2570
    

    【讨论】:

    • 感谢 Dealec,这是完美且最简单的方法:)
    猜你喜欢
    • 1970-01-01
    • 2022-12-02
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    相关资源
    最近更新 更多