-1

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")
jpsmith
  • 11,023
  • 5
  • 15
  • 36

1 Answers1

0

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')
jay.sf
  • 60,139
  • 8
  • 53
  • 110