从列表中的行匹配的数据框中删除行

Remove Rows From Data Frame where a Row match from a list

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

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 ))

这工作正常,但实际上我的列表很长而且是动态的,我想将排除列表存储在一个列表中,并想合并这两个列表,使所有帐户都列在排除列表中,但不确定如何操作。任何人都可以帮助我

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

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

您可以使用 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