date format error message in R

I combined datasets and I run the following function but it returned an error message indicated below. I also run a str function and I observed that the date format is displayed as mdy as is on the original datasets and the result for a numeric value is NA

all_trips$ride_length<-difftime(all_trips$ended_at,all_trips$started_at)
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
4/1/2019 0:02" "4/1/2019 0:03" "4/1/2019
num NA ..

The difftime() function only takes POSIXct or date class objects as arguments and you are passing string class values. You need to convert them first, take a look at this example:

difftime(as.POSIXct("4/1/2019 0:03", format = "%m/%d/%Y %H:%M", tz = "UTC"),
         as.POSIXct("4/1/2019 0:02", format = "%m/%d/%Y %H:%M", tz = "UTC"))
#> Time difference of 1 mins

Created on 2022-09-18 with reprex v2.0.2

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.