I have a dataset where I am using difftime to calculate the difference between two times in R. For 4 of the records that start in one day and continue past midnight I am getting nonsense answers.
Dataset
time2<-dput(time2)
structure(list(StationID = c(201707123L, 201710032L, 201710148L,
201710188L), TowStartTime = structure(c(-2209057289, -2209057558,
-2209057779, -2209057812), class = c("POSIXct", "POSIXt"), tzone = ""),
TowEndTime = structure(c(-2209142790, -2209143047, -2209143555,
-2209143587), class = c("POSIXct", "POSIXt"), tzone = ""),
tow_time = c(-1425.01666666667, -1424.81666666667, -1429.6,
-1429.58333333333)), .Names = c("StationID", "TowStartTime",
"TowEndTime", "tow_time"), row.names = c(572L, 783L, 1003L, 1079L
), class = "data.frame")
Code used to calculate tow_tim
time2$tow_time<-as.numeric(difftime(strptime(time2$TowEndTime,"%Y-%m-%d %H:%M:%S"),
strptime(time2$TowStartTime,"%Y-%m-%d %H:%M:%S")),units="mins")
I have the end time in front of the start time because I didnt want to have negative values. I get the same answers for the time difference either way its just not a negative value. Is there a way to account for going over midnight with difftime or another function in R to do this?