Select row with most recent date by group

后端 未结 5 1960
南方客
南方客 2020-12-01 14:42

I have a data frame in R where the rows represent events, and one column is the date of the event. The thing the event is happening to is described by an ID column. So for e

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 15:33

    I've never processed any data in R without plyr!

    library(plyr)
    ddply(df, .(ID), summarize, most_recent = max(as.Date(date, '%m/%d/%Y')))
    
       ID most_recent
    1  1  2001-03-14
    2  2  2008-02-01
    3  3  2011-08-22
    

提交回复
热议问题