calculating number of days between 2 columns of dates in data frame

后端 未结 5 1459
轮回少年
轮回少年 2020-12-02 12:56

I have a data frame which has two columns of dates in the format yyyy/mm/dd. I am trying to calculate the number of days between these two dates for each observation within

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 13:45

    Without your seeing your data (you can use the output of dput(head(survey)) to show us) this is a shot in the dark:

    survey <- data.frame(date=c("2012/07/26","2012/07/25"),tx_start=c("2012/01/01","2012/01/01"))
    
    survey$date_diff <- as.Date(as.character(survey$date), format="%Y/%m/%d")-
                      as.Date(as.character(survey$tx_start), format="%Y/%m/%d")
    survey
           date   tx_start date_diff
    1 2012/07/26 2012/01/01  207 days
    2 2012/07/25 2012/01/01  206 days
    

提交回复
热议问题