删除除最后一个实例之外的所有重复项 [英] Remove all duplicates except last instance
问题描述
所以我在 R 中有一个数据集,以以下布局为例:
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
我想删除除帖子 ID 的最后一个实例之外的所有实例.现在我可以在网上找到所有东西,我正在使用的功能正在删除除第一个实例之外的所有内容.
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?
推荐答案
使用 !rev(duplicated(rev(ID)))
过滤除最后唯一出现的所有事件.
Use !rev(duplicated(rev(ID)))
to filter out all but the last unique occurrences.
要过滤数据集,请使用 dataset[!rev(duplicated(rev(dataset$ID))),]
To get the dataset filtered, use dataset[!rev(duplicated(rev(dataset$ID))),]
这篇关于删除除最后一个实例之外的所有重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!