删除所有唯一的行 [英] Remove all unique rows
本文介绍了删除所有唯一的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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,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
子集(df,重复(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,因为他们都有相同的col 1.如何获得1和6被包括在内?或者相反,如何删除没有重复的行?
解决方案
另一个选项:
子集(df,重复(col1)|重复(col1,fromLast = TRUE))
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
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?
解决方案
Another option:
subset(df,duplicated(col1) | duplicated(col1, fromLast=TRUE))
这篇关于删除所有唯一的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文