Search code examples
rdifftime

Calulcate running difference of time using difftime on one column of timestamps


How would you calculate time difference of two consecutive rows of timestamps in minutes and add the result to a new column.

I have tried this:

data$hours <- as.numeric(floor(difftime(timestamps(data), (timestamps(data)[1]), units="mins")))

But only get difference from time zero and onwards.

Added example data with 'mins' column that I want to be added

timestamps                        mins
2013-06-23 00:00:00               NA
2013-06-23 01:00:00               60
2013-06-23 02:00:00               60
2013-06-23 04:00:00              120

Solution

  • The code that you're using with the [1] is always referencing the first element of the timestamps vector.

    To do what you want, you want to look at all but the first element minus all but the last element.

    mytimes <- data.frame(timestamps=c("2013-06-23 00:00:00",
                                       "2013-06-23 01:00:00",
                                       "2013-06-23 02:00:00",
                                       "2013-06-23 04:00:00"),
                          mins=NA)
    mytimes$mins <- c(NA, difftime(mytimes$timestamps[-1],
                                   mytimes$timestamps[-nrow(mytimes)],
                                   units="mins"))
    

    What this code does is:

    1. Setup a data frame so that you will keep the length of the timestamps and mins the same.
    2. Within that data frame, put the timestamps you have and the fact that you don't have any mins yet (i.e. NA).
    3. Select all but the first element of timestamps mytimes$timestamps[-1]
    4. Select all but the last element of timestamps mytimes$timestamps[-nrow(mytimes)]
    5. Subtract them difftime (since they're well-formatted, you don't first have to make them POSIXct objects) with the units of minutes. units="mins"
    6. Put an NA in front because you have one fewer difference than you have rows c(NA, ...)
    7. Drop all of that back into the original data frame's mins column mytimes$mins <-