0

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.

amonk
  • 1,769
  • 2
  • 18
  • 27
Samuel Ellett
  • 37
  • 1
  • 1
  • 4
  • Please don't post images of your data. Instead, post the data itself, in a form that can be copied by other users to reproduce your situation. – RHertel Jun 15 '16 at 04:37
  • 1
    So there is one row having time difference 0 (or `NA`) as it has no "predecessor". Can't you inject/prepend such a padding value? If ok with semantics, that is always the way to go with dervied differencing columns, isn't it? But I may well miss some detail here.Also I am not able to play around with the problem input, as @RHertel nicely pointed out ;-) – Dilettant Jun 15 '16 at 04:41
  • Thanks @RHertel for the tip on posting. I'll make sure to include an example that can be manipulated. – Samuel Ellett Jun 15 '16 at 04:53

1 Answers1

2

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.

RHertel
  • 23,412
  • 5
  • 38
  • 64
  • 1
    You could simplify the `diff` bit as it will return a `difftime` by default, which can be converted to weeks like `c(NA,\`units<-\`(diff(x),"weeks"))` – thelatemail Jun 15 '16 at 04:55
  • Excellent, @thelatemail ! Do you mind if I include your suggestion in the answer? – RHertel Jun 15 '16 at 04:57