2

I get the above error when trying to find an interval between two days.

x <- as.POSIXct(strptime(as.character(wo_data$In.Progress.time),"%m/%d/%Y %I:%M:%S %p"))

class(x)

# [1] "POSIXct" "POSIXt" 

wo_data$super.schedule.inprogress <-
as.numeric(difftime(x, wo_data$schedule.time, units = "hours"))  

# Error in as.POSIXct.default(time1) : 
# do not know how to convert 'time1' to class POSIXct
# In addition: Warning messages:
# 1: Too many values at 103 locations: 608, 617, 659, 724, 753, 754, 755, 
#    802, 1029, 1030, 1031, 1037, 1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, ... 
# 2: Too few values at 3170 locations: 60, 109, 110, 112, 114, 118, 119, 
#    120, 121, 122, 161, 163, 167, 172, 197, 202, 206, 207, 208, 209, ... 
# 3: 3 failed to parse. 

Any ideas why it's happening even though R successfully converted wo_data$In.Progress.time into a vector of class time?

Thank you in advance, Eli

Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40
eliran
  • 21
  • 1
  • 2
  • 3
    _Both_ arguments to `difftime()` must be `POSIXt`. You showed us one but not the other. – Dirk Eddelbuettel Dec 04 '17 at 23:09
  • class(wo_data$Schedule.time) [1] "POSIXlt" "POSIXt" --- I'm not concered about time2 b/c 'In.Progress' is the only time variable I couldn't calculate intervals with (I managed to do it with other 7 different ones and 'Schedule.time' is one of them). – eliran Dec 04 '17 at 23:25
  • 1
    `wo_data$Schedule.time` is not the same as `wo_data$schedule.time` - case sensitivity and all that. I can replicate your error with `df <- data.frame(one=1); difftime(Sys.time(), df$not_existing)` , which suggests you are not referencing the variable properly. – thelatemail Dec 04 '17 at 23:33
  • it was a type. Here is a proof that wo_data$Schedule.time works--- > as.numeric(difftime(wo_data$Completed.time,wo_data$Schedule.time, units = "hours"))[9987:9991] [1] 0.01111111 98.06666667 NA NA 0.27805556 > #don't work > wo_data$super.schedule.inprogress =as.numeric(difftime(wo_data$In.progress.time,wo_data$Schedule.time, units = "hours")) Error in as.POSIXct.default(time1) : do not know how to convert 'time1' to class “POSIXct” – eliran Dec 04 '17 at 23:56

1 Answers1

0

Try starting with the two dates as character data, then convert them on-the-fly into POSIXct. I'd need some example data to help you troubleshoot any further. I had a similar problem and the following code worked for me.

as.numeric(difftime(as.POSIXct(end_date), as.POSIXct(start_date)))
Oscar Montoya
  • 611
  • 4
  • 8