Search code examples
rtimeextract

Turning text with hours and minutes to minutes


I have got a big dataframe that look like this:

from to distance in km traveltime
1033 1076 1.8 6 mins
1035 1076 2.6 1 min
1036 1076 2.4 1 hour 26 mins
1037 1076 6.7 3 hours 1 min

i want get the traveltime column to show the traveltime in minutes, so like this:

from to distance in km traveltime
1033 1076 1.8 6
1035 1076 2.6 1
1036 1076 2.4 86
1037 1076 6.7 181

I have tried the hm() function but it doesn't work on the values which are smaller than 1 hour.


Solution

  • Try this wrapping a time transformation in nested replacements for strings:

    #Code
    df$NewTime <- unname(sapply(sub('s','',sub("\\s+min",
                      "", sub("hour|hours", "* 60 +", df$traveltime))), 
                  function(x) eval(parse(text=x))))
    

    Output:

    df
      from   to distance.in.km     traveltime NewTime
    1 1033 1076            1.8         6 mins       6
    2 1035 1076            2.6          1 min       1
    3 1036 1076            2.4 1 hour 26 mins      86
    4 1037 1076            6.7  3 hours 1 min     181
    

    Some data used:

    #Data
    df <- structure(list(from = c(1033L, 1035L, 1036L, 1037L), to = c(1076L, 
    1076L, 1076L, 1076L), distance.in.km = c(1.8, 2.6, 2.4, 6.7), 
        traveltime = c("6 mins", "1 min", "1 hour 26 mins", "3 hours 1 min"
        )), row.names = c(NA, -4L), class = "data.frame")
    

    Another option using stringr:

    library(stringr)
    #Code 2
    df$NewTime <- sapply(str_extract_all(df$traveltime, "\\d+"), function(x) {
      x1 <- as.numeric(x)
      if(length(x1)>1) x1[1]*60 + x1[2] else x1 })
    

    It will produce the same output.