R:标识重复的行并删除旧条目(按日期) [英] R: identify duplicate rows and remove the old entry(By Date)

查看:90
本文介绍了R:标识重复的行并删除旧条目(按日期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下形式的数据框:

I have a dataframe of the following form:

   ID    value    modified
1  AA    30       2016-11-03
2  AB    40       2016-11-04
3  AC    50       2016-11-05
4  AA    60       2016-11-06
5  AB    20       2016-11-07

我想标识ID列的所有重复行,并删除修改时间相对较旧的行。因此输出为:

I want to identify all the duplicate rows for ID column and remove rows which has comparatively old modification time. So the output will be:

   ID    value    modified
1  AC    50       2016-11-05
2  AA    60       2016-11-06
3  AB    20       2016-11-07

代码我正在尝试如下:

ID<-c('AA','AB','AD','AA','AB')
value<-c(30,40,50,60,20)
modified<-c('2016-11-03','2016-11-04','2016-11-05','2016-11-06','2016-11-07')
df<-data.frame(ID=ID,value=value,modified=modified)
df
  ID value   modified
1 AA    30 2016-11-03
2 AB    40 2016-11-04
3 AD    50 2016-11-05
4 AA    60 2016-11-06
5 AB    20 2016-11-07

df[!duplicated(df$ID),]
  ID value   modified
1 AA    30 2016-11-03
2 AB    40 2016-11-04
3 AD    50 2016-11-05

但这不是我想要的输出,如何删除旧条目?预先感谢您提供任何线索或提示。

But this is not my desired output, how can I remove the old entries? Thank you in advance for any clue or hints.

推荐答案

您可以按以下方式使用dplyr软件包:

You can use the dplyr package as follows:

library(dplyr)
library(magrittr)

df %<>% group_by(ID) %>% filter(modified==max(modified))

如果要在新变量

library(dplyr)

df2 <- df %>% group_by(ID) %>% filter(modified==max(modified))

这篇关于R:标识重复的行并删除旧条目(按日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆