删除所有唯一行 [英] Remove all unique rows
本文介绍了删除所有唯一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图弄清楚如何从数据框中删除所有唯一行,但如果它有重复,我希望保留它.例如 - 我希望其中的所有列与 col1 相同:
I am trying to figure out how to remove all unique rows, from a data frame, but if it has a duplicate, I want that to stay in. For Example - I want all columns from this with col1 the same:
df<-data.frame(col1=c(rep("a",3),"b","c",rep("d",3)),col2=c("A","B","C",rep("A",3),"B","C"),col3=c(3,3,1,4,4,3,2,1))
df
col1 col2 col3
1 a A 3
2 a B 3
3 a C 1
4 b A 4
5 c A 4
6 d A 3
7 d B 2
8 d C 1
subset(df,duplicated(col1))
col1 col2 col3
2 a B 3
3 a C 1
7 d B 2
8 d C 1
但我想要第 1、2、3、6、7、8 行,因为它们都有相同的第 1 列.如何让第 1 行和第 6 行包含在内?或者,相反,如何删除没有重复的行?
But I want to have rows 1,2,3,6,7,8 since they all have the same col 1. How do I get 1 and 6 to be included? Or, conversely, how do I remove rows that do not have a duplicate?
推荐答案
另一种选择:
subset(df,duplicated(col1) | duplicated(col1, fromLast=TRUE))
这篇关于删除所有唯一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文