I have a data set that looks like this:
ID | DATE | SCORE ------------------------- 123 | 1/15/10 | 10 123 | 1/1/10 | 15 124 | 3/5/10 |
In the case where you need a more complex formula, you can use aggregate:
a <- aggregate(date ~ id, data=data, FUN=function(x) c(NA,diff(x))) data$dayssincelast <- c(t(a[-1]), recursive=TRUE) # Remove 'id' column
The same sort order applies here as in @nograpes answer.