Calculate number of days between two dates in r

后端 未结 4 1653
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 23:22

I need to calculate the number of days elapsed between multiple dates in two ways and then output those results to new columns: i) number of days that has elapsed as compare

4条回答
  •  猫巷女王i
    2020-12-17 23:44

    You can just add each column with the simple difftime and lagged diff calculations.

    DATA$FIRST <- c(0, 
                    with(DATA, 
                         difftime(DATE[2:length(DATE)],DATE[1], unit="days")
                         )
                    )
    DATA$BETWEEN <- c(0, 
                      with(DATA, 
                           diff(DATE[1:(length(DATE) - 1)], unit="days")
                           )
                      )
    
    identical(DATA, RESULTS)
    [1] TRUE
    

提交回复
热议问题