I have a data frame that looks like this:
id date
1001 2012-10-11
1005 2013-02-20
1005 2012-11-21
1005 2014-03-14
1003 2013-10-25
1003 2013-11-30
I need to find, for each row, the number of days that have passed since the last occurrence of that id. For the above example, the answer would look like this:
id date no_of_days
1001 2012-10-11 NA
1005 2013-02-20 91
1005 2012-11-21 NA
1005 2014-03-14 387
1003 2013-10-25 NA
1003 2013-11-30 36
A bit of searching got me to the point where I can add a new column with values that were generated by applying a function on subgroups (the R equivalent of Ststa's bysort
):
df$no_of_days<-with(df,ave(id,id,FUN=days_passed,na.rm=TRUE))
However, defining the new function days_passed is proving to be tricky as I have to find the last occurrence of that uniqid, and then formulate the function accordingly.
I'm new to R, so any help on this would be greatly appreciated.