Remove all duplicates except last instance

删除回忆录丶 提交于 2019-11-26 23:20:20

问题


So I have a dataset in R with the following layout as an example:

ID Date Tally
1 2/1/2011 1
2 2/1/2011 2
3 2/1/2011 3
1 2/1/2011 4
2 2/1/2011 5
1 2/1/2011 6
3 2/1/2011 7
4 2/1/2011 8
2 2/1/2011 9

I want to remove all instances except the LAST instance of the post id. Right now everything I can find online, and functions I am using is removing everything except the FIRST instance.

So my new data frame would look like:

ID Date Tally
1 2/1/2011 6
3 2/1/2011 7
4 2/1/2011 8
2 2/1/2011 9

How do I do this? Right now I am only able to keep the first instance. I want it to do the opposite? Any help?

Bear with me I am new to R :)


回答1:


Use !rev(duplicated(rev(ID))) to filter out all but the last unique occurrences.

To get the dataset filtered, use dataset[!rev(duplicated(rev(dataset$ID))),]




回答2:


Wouldn't this just be the standard case for using the 'fromLast' parameter to duplicated?

 dat[ !duplicated(dat[, c("ID", "Date")], fromLast=T),]
#---------
  ID     Date Tally
6  1 2/1/2011     6
7  3 2/1/2011     7
8  4 2/1/2011     8
9  2 2/1/2011     9

Your example was not rich enough to tell whether you needed the "Date" column in the test fro duplication, so perhaps you could simplify. I'm leaving it in to illustrate that duplicated has a data.frame method. I prefer !duplicated to unique because it allows easy access to the set complement if you are comparing groups.




回答3:


Using a data.table join, you can set mult = 'last'

For example

library(data.table)
DT <- data.table(DF, key = 'id')

# join with the unique ID values
DT[unique(DT[,list(ID)]), mult= 'last']

   ID     Date Tally
1:  1 2/1/2011     6
2:  2 2/1/2011     9
3:  3 2/1/2011     7
4:  4 2/1/2011     8

If you knew the unique IDs you could also any of the following

DT[.(1:4), mult='last']
DT[list(1:4), mult = 'last']



回答4:


Use dplyr:

data <- data %>%
  group_by(ID) %>%
  slice(which.max(Tally))


来源:https://stackoverflow.com/questions/15641924/remove-all-duplicates-except-last-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!