0

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?

user41509
  • 978
  • 1
  • 10
  • 31

2 Answers2

0

There is not an error, in that cases you have a TowStartTime after a TowEndTime, so tow_time will be negative.

Example:

StationID        TowStartTime          TowEndTime  tow_time
572  201707123 1899-12-31 05:58:31 1899-12-30 06:13:30 -1425.017

In hour you can see that EndTime is 23 hour before startTime:

-1425.017/60
[1] -23.75028

This output is not wrong, maybe original data are. You have negative time stamp that are convertend in uncommon date:

as.POSIXct(-2209143587, origin="1970-01-01")
[1] "1899-12-30 06:00:13 CET"
Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39
  • It doesn't matter if you change the code to start time minus end time you still get the same value without a negative symbol in front of it 1429. Both time columns have a weird date in front of them. Those dates are not the dates. I have another column with the correct dates - should I replace the incorrect dates and run the same code? – user41509 Mar 13 '18 at 15:45
  • You have negative time stamp that are convertend in uncommon date: as.POSIXct(-2209143587, origin="1970-01-01") [1] "1899-12-30 06:00:13 CET" – Terru_theTerror Mar 13 '18 at 15:55
0

You could consider to use the interval function from the lubridate package.

However, it is still strange your endtimes are after your starttimes.

 > interval(df1$TowStartTime, df1$TowEndTime) %/% minutes(1)
 [1] -1425 -1424 -1429 -1429
Lennyy
  • 5,932
  • 2
  • 10
  • 23