[英]How to remove rows from a data frame using a subset?
I have a column in a data frame called Retest_data that goes like this:我在名为 Retest_data 的数据框中有一列,如下所示:
SFC
YU006UGD31092
YU006UGD31071
YU006UGD30152
YU006UGD25831
YU006UGD25831
YU006UGD25332
YU006UG922912
YU006UG922912
And what I want is to remove all instances of values that occur more than once.我想要的是删除出现不止一次的所有值实例。 So dplyr functions like unique and distinct won't work for me.所以像 unique 和 distinct 这样的 dplyr 函数对我不起作用。
I also have a list called Remove_SFC that has all the SFC values that occur more than once.我还有一个名为 Remove_SFC 的列表,其中包含多次出现的所有 SFC 值。 How can I use this list to remove all recurring values from my data?如何使用此列表从我的数据中删除所有重复值? Thanks.谢谢。
Data:数据:
df <- data.frame(SFC = c("YU006UGD31092","YU006UGD31071",
"YU006UGD30152",
"YU006UGD25831",
"YU006UGD25831",
"YU006UGD25332" ,
"YU006UG922912",
"YU006UG922912"))
Code:代码:
df %>%
group_by(SFC) %>%
filter(n() == 1)
Output:输出:
SFC
<chr>
1 YU006UGD31092
2 YU006UGD31071
3 YU006UGD30152
4 YU006UGD25332
Edit:编辑:
If you have the list, you can also do:如果您有列表,您还可以执行以下操作:
df %>%
filter(!(SFC %in% Remove_SFC))
As an alternative you can use dplyr
's anti_join
.作为替代方案,您可以使用dplyr
的anti_join
。 anti_join
return all rows from df
without a match in Remove_SFC
: anti_join
从df
返回所有行,而在Remove_SFC
没有匹配Remove_SFC
:
library(dplyr)
df %>%
anti_join(data.frame(SFC=Remove_SFC))
which returns返回
Joining, by = "SFC"
SFC
1 YU006UGD31092
2 YU006UGD31071
3 YU006UGD30152
4 YU006UGD25332
Data数据
Remove_SFC <- c("YU006UG922912", "YU006UGD25831")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.