Earliest Date for each id in R

前端 未结 4 1728
余生分开走
余生分开走 2020-12-31 18:54

I have a dataset where each individual (id) has an e_date, and since each individual could have more than one e_date, I\'m trying to get the earli

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 19:28

    You may use library(sqldf) to get the minimum date as follows:

    data1<-data.frame(id=c("789","123","456","123","123","456","789"),
                      e_date=c("2016-05-01","2016-07-02","2016-08-25","2015-12-11","2014-03-01","2015-07-08","2015-12-11"))  
    
    library(sqldf)
    data2 = sqldf("SELECT id,
                        min(e_date) as 'earliest_date'
                        FROM data1 GROUP BY 1", method = "name__class")    
    
    head(data2)   
    

    id earliest_date
    123 2014-03-01
    456 2015-07-08
    789 2015-12-11

提交回复
热议问题