Search code examples
rtimetimestamptransformminute

Convert time format with "x hours y minutes" to numeric


I have a variable indicating time since event and it is coded like this:

time_since_event <- c("18 m", "56 m", "2 h 30 m", "18 h 2 m")

I want it to be numeric and to always indicate the number of minutes since the event:

time_since_event_min <- c(18,56,150,1082)

Solution

  • You can use lubridate functions :

    library(lubridate)
    (time_since_event %>% toupper %>% period %>% period_to_seconds())/60
    #[1]   18   56  150 1082