Is there an R function to subtract two date time data?

63 Views Asked by At

I have two columns of data that are dttm that I'm trying to get the difference from to then get the total average.

Essentially I have a start time to a trip, an end time. I'd like to know the difference between them and then get the average time that produces, but difftime() isn't working. Any ideas on what function to use for two dttm to return a value in hours, min, sec?

Error in as.POSIXct.default(time1, tz = tz) : do not know how to convert 'time1' to class “POSIXct”

This error is thrown when I try to enter just the difftime(ended_at,started_at)

cyclistic_data %>% 
  group_by(member_casual) %>% 
  drop_na() %>% 
  summarise(av_member_ride_time = mean(difftime(ended_at,started_at)))

EDIT: I may have found a way to achieve what I want out of that one line anyways.

cyclistic_data %>% 
  reframe(av_member_ride_time = mean(as_hms(difftime(started_at, ended_at))))
0

There are 0 best solutions below