Search code examples
rdataframedifftime

Time period between dates in R


I have a data frame with Id Column & Date Column.

enter image description here

Essentially, I would like to create a third column (Diff) that calculates the difference between dates, preferably grouped by ID.

I have constructed a large POSIXlt from the following code

c_time <- as.POSIXlt(df$Gf_Date)
a <- difftime(c_time[1:(length(c_time)-1)], c_time[2:length(c_time)], units = weeks")

However when I try cbind onto my data.frame it errors

"arguments imply differing number of rows"

as a is one row shorter than the original data.frame.

Any help would be greatly appreciated.


Solution

  • Since the difference can only be taken between two subsequent dates, it is undefined for the first entry. Therefore a reasonable choice would be to set that first value to NA.

    This might work:

    c_time <- as.POSIXlt(df$Gf_Date)
    a <- c(NA,`units<-`(diff(c_time),"weeks"))
    cbind(df,diff.dates=a)
    

    (Hat tip to @thelatemail for a valuable suggestion to simplify the definition of a).

    PS: Note that the differences in a may have a different sign compared to your original approach. Depending on the convention that you prefer, you can use a <- -a to convert between the two.