Convert calculated time from seconds to minutes

HI Everyone,

Im new to R (and programming in general) so any help is much appreciated. In my dataframe (Data_Trip) , I've calculated the difference between two datetimes but want to convert the output from seconds to minutes. The new column is named time_rented.

Here is my current code:

Trip_Data$time_rented <- Trip_Data$ended_at - Trip_Data$started_at


For almost all things time-related the {lubridate} package is a sanity preserver. If your data had start/finish as variables, for example

#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>     date, intersect, setdiff, union
# recorded as character class in data
begin <- "2022-10-22 14:43:04"
finish <- "2022-10-22 14:03:44"
# convert to datetime class
begin <- ymd_hms(begin)
finish <- ymd_hms(finish)
(elapsed <- finish - begin)
#> Time difference of -39.33333 mins

But, let's work with what you've got, which is a variable, time_rented of character strings.

time_rented <- as.integer(gsub(" secs$","",time_rented))

assuming that the seconds are all integers and secs is consistent.

Thank you for your help!

This topic was automatically closed 7 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.