2013-10-23 54 views
3
ID Julian Month Year Location Distance 
2 40749 July 2011  8300 39625 
2 41425 May 2013 Hatchery 31325 
3 40749 July 2011  6950 38625 
3 41057 May 2012 Hatchery 31325 
6 40735 July 2011  8300 39650 
12 40743 July 2011 11025 42350 

以上是我正在使用的数据框的head()。它包含超过7000行和3000个唯一的ID值。我想删除所有只有一个ID值的行。这可能吗?也许解决方案只保留ID重复的行?只选择在r中具有相同ID的行

回答

1

这里是我会怎么做:

new.dataframe <- c() 
ids <- unique(dataframe$ID) 
for(id in ids){ 
temp <- dataframe[dataframe$ID == id, ] 
if(nrow(temp) > 1){ 
new.dataframe <- rbind(new.dataframe, temp) 
}} 

这将删除所有只有一行的ID

5

如果d是你的数据帧,我会使用duplicated找到行有重复的ID。使用fromLast中的两个参数可以获得第一个和最后一个重复ID行。

d[(duplicated(d$ID, fromLast = FALSE) | duplicated(d$ID, fromLast = TRUE)),] 

这双精度duplicated方法有多种用途:

Finding ALL duplicate rows, including "elements with smaller subscripts"

How to get a subset of a dataframe which only has elements which appear in the set more than once in R

How to identify "similar" rows in R?

+0

作品完美,感谢您的回复迅速! – user2909729

相关问题