rtimestampstrptimedifftime

Calculate time difference between 2 timestamps in hours using R


I'm trying to obtain the time difference between 2 timestamps in hours.

I have the data:

ID      Lat          Long    Traffic        Start_Time        End_Time
1      -80.424      40.4242    54       2018-01-01 01:00   2018-01-01 01:10
2      -80.114      40.4131    30       2018-01-01 02:30   2018-01-01 02:40
3      -80.784      40.1142    12       2018-01-01 06:15   2018-01-01 07:20

I want to get the data like this

ID      Lat          Long    Traffic        Start_Time        End_Time        differ_hrs 

1      -80.424      40.4242    54       2018-01-01 01:00   2018-01-01 01:10    00:50
2      -80.114      40.4131    30       2018-01-02 08:30   2018-01-02 08:40    01:10
3      -80.784      40.1142    12       2018-01-04 19:26   2018-01-04 20:11    01:15

I tried this code to capture the difference in hours:

df$differ_hrs<- difftime(df$End_Time, df$Start_Time, units = "hours")

However, it captures the difference like this:

    ID      Lat          Long    Traffic        Start_Time        End_Time        differ_hrs 

    1      -80.424      40.4242    54       2018-01-01 01:00   2018-01-01 01:10    0.5
    2      -80.114      40.4131    30       2018-01-02 08:30   2018-01-02 08:40    0.70
    3      -80.784      40.1142    12       2018-01-04 19:26   2018-01-04 20:11    0.75

then I tried to set the difference in hours into format="%H%M" using the code:

df$differ_HHMM<- format(strptime(df$differ_hrs, format="%H%M"), format = "%H:%M")

But it produces all NAs.

So I decided to try a different way where I calculate the difference and set the format in the command itself adding "%H%M" like this:

df$differ_HHMM<- as.numeric(difftime(strptime(paste(df[,6]),"%Y-%m-%d %H:%M:%S"), strptime(paste(df[,5]),"%Y-%m-%d %H:%M:%S"),format="%H%M",  units = "hours"))

but I keep getting the error message:

Error in difftime(strptime(paste(df[, 6]), "%Y-%m-%d %H:%M:%S"), strptime(paste(df[,  : 
  unused argument (format = "%H:%M:%S")

Is there any way to calculate the time difference in %H:%M format?

I really appreciate your suggestions


Solution

  • The difference is a difftime class built on top of numeric. We could specify the units in difftime as seconds and use seconds_to_period from lubridate

    library(lubridate)
    df$differ_hrs<- as.numeric(difftime(df$End_Time, df$Start_Time, 
            units = "secs"))
    out <- seconds_to_period(df$differ_hrs)
    df$differ_HHMM <- sprintf('%02d:%02d', out@hour, out$minute)
    

    NOTE: format works only on Date or Datetime class i.e. POSIXct, POSIXlt and not on numeric/difftime objects

    data

    df <- structure(list(ID = 1:3, Lat = c(-80.424, -80.114, -80.784), 
        Long = c(40.4242, 40.4131, 40.1142), Traffic = c(54L, 30L, 
        12L), Start_Time = structure(c(1514786400, 1514791800, 1514805300
        ), class = c("POSIXct", "POSIXt"), tzone = ""), End_Time = structure(c(1514787000, 
        1514792400, 1514809200), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, 
    -3L), class = "data.frame")