Select row with most recent date by group

后端 未结 5 1884
南方客
南方客 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:25

    You can try

    library(dplyr)
    df %>% 
      group_by(ID) %>%
      slice(which.max(as.Date(date, '%m/%d/%Y')))
    

    data

    df <- data.frame(ID= rep(1:3, each=3), date=c('02/20/1989',
    '03/14/2001', '02/25/1990',  '04/20/2002', '02/04/2005', '02/01/2008',
    '08/22/2011','08/20/2009', '08/25/2010' ), stringsAsFactors=FALSE)
    

提交回复
热议问题