Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format in difftime function

165 Views Asked by At

I am applying difftime function on the columns started_at and ended_at and the data type of both columns is "character" but i am getting the following error.

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

enter image description here

I tried this code

all_trips$ride_length <- difftime(all_trips$ended_at, all_trips$started_at, units = "secs")
1

There are 1 best solutions below

0
jay.sf On

Use strptime to get POSIX format first.

difftime(strptime('4/1/2019 0:02', '%m/%d/%Y %H:%M'), strptime('4/1/2019 0:09', '%m/%d/%Y %H:%M'), units='secs')
# Time difference of -420 secs

I.e.

difftime(strptime(all_trips$ended_at, '%m/%d/%Y %H:%M'), strptime(all_trips$started_at, '%m/%d/%Y %H:%M'), units='secs')