I am trying to apply diff() on a series of columns containing dates. I am interested in the difference between date1-date2, date2-date3, etc.
I am interested in:
- the actual difference between the dates (days)
- if all dates of a row are in order (diff >= 0, by row)
I can use diff() on a series of dates (e.g. on the first row --> diff(unlist(df1[1,])) ). I just need to apply this per row, i guess using apply(), but for some reason I can't work it out. Some dates are missing, which is allowed in my study.
Hopefully this is very easy for you guys...
df <- structure(list(date1 = structure(c(-10871, -13634, -15937, -15937,
-290, -2323), class = "Date"), date2 = structure(c(16678, NA,16037, 16659,
16538, 16626), class = "Date"), date3 = structure(c(16685,16688, NA, 16659,
16568, 16672), class = "Date"), date4 = structure(c(16701, 16695, 16670,
16661, 16582, 16672), class = "Date"), date5 = structure(c(16709, 16695,
16661, 16667, 16619, 16692), class = "Date")), .Names = c("date1","date2",
"date3", "date4", "date5"), row.names = c("2", "3", "4", "5", "6", "7"),
class = "data.frame")
df
applyconverts everything tocharacter, causingdiffto fail. - thelatemaildiff), you just needsapply(df, diff). - alistairet(apply(df, 1, function(x){diff(as.Date(x))})), though you'll lose your column names. Equivalent, but a little uglier, and keeps column names:t(sapply(1:nrow(df), function(x){diff(unlist(df[x,]))}))- alistaire