在多行中的单列中找到最大日期 [英] Find the max date in a single column across multiple rows

查看:63
本文介绍了在多行中的单列中找到最大日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

id       <- c(1,1,2,3,3)
date     <- c("23-01-08","01-11-07","30-11-07","17-12-07","12-12-08")
df       <- data.frame(id,date)
df$date2 <- as.Date(as.character(df$date), format = "%d-%m-%y")


id     date      date2
1   23-01-08 2008-01-23
1   01-11-07 2007-11-01
2   30-11-07 2007-11-30
3   17-12-07 2007-12-17
3   12-12-08 2008-12-12

现在我需要创建第四列并为每个 id 在那。
决赛桌应如下:

now I need to create a forth column and insert maximum date of transaction for each id in that. the final table should be as follow:

id     date      date2        max
1   23-01-08 2008-01-23 2008-01-23
1   01-11-07 2007-11-01   0
2   30-11-07 2007-11-30 2007-11-30 
3   17-12-07 2007-12-17   0
3   12-12-08 2008-12-12 2008-12-12

如果您能帮助我,将不胜感激。

I would be thankful if you could help me with this.

推荐答案

id<-c(1,1,2,3,3)
date<-c("23-01-08","01-11-07","30-11-07","17-12-07","12-12-08")
df<-data.frame(id,date)
df$date2<-as.Date(as.character(df$date), format = "%d-%m-%y")
# aggregate can be used for this type of thing
d = aggregate(df$date2,by=list(df$id),max)
# And merge the result of aggregate 
# with the original data frame
df2 = merge(df,d,by.x=1,by.y=1)
df2

  id     date      date2          x
1  1 23-01-08 2008-01-23 2008-01-23
2  1 01-11-07 2007-11-01 2008-01-23
3  2 30-11-07 2007-11-30 2007-11-30
4  3 17-12-07 2007-12-17 2008-12-12
5  3 12-12-08 2008-12-12 2008-12-12

编辑:由于您希望当日期与最大值不匹配时最后一列为空日期,您可以尝试下一行。

Since you want the last column to be "empty" when the date does not match the max date, you can try the next line.

df2[df2[,3]!=df2[,4],4]=NA

df2
  id     date      date2          x
1  1 23-01-08 2008-01-23 2008-01-23
2  1 01-11-07 2007-11-01       <NA>
3  2 30-11-07 2007-11-30 2007-11-30
4  3 17-12-07 2007-12-17       <NA>
5  3 12-12-08 2008-12-12 2008-12-12

当然,清理名字等总是很好,但是我留给您。

Of course, it is always nice to clean up the colnames, etc., but I leave that for you.

这篇关于在多行中的单列中找到最大日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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