How to transform a time data from 01:30 to 90 minutes in R

-4

I have a column with data for hours, I would like to turn those hours into minutes.

T.Real <- c("08:42","08:33","00:41","01:11","01:35","00:45","01:43","01:03","01:06")
    
asked by anonymous 27.09.2018 / 14:52

1 answer

3

Using the following vector as an example

horas <- c("1:00:00", "0:45:00", "0:30:00", "1:30:00")

Here are two options:

  • using the package chron

    library(chron)
    ch <- times(horas)
    60 * hours(ch) + minutes(ch)
    
  • using the package lubridate

    library(lubridate)
    res <- hms(horas)
    hour(res)*60 + minute(res)
    

The times and hms functions transform the horas object into a time format.

To add these values as a new variable in your database, you can:

  • using base R

    novo.GT.Real <- as.character(tempodesolo$GT.Real) 
    res <- hm(novo.GT.Real) 
    tempodesolo$novo.GT.Real <- hour(res)*60 + minute(res)
    
  • using the dplyr package - I'll sample with several mutate for you to understand step by step. You could put everything inside a single mutate

    tempodesolo <- tempodesolo  %>%
      mutate(novo.GT.Real = as.character(GT.Real))  %>%
      mutate(novo.GT.Real = hm(novo.GT.Real))  %>%
      mutate(novo.GT.Real = hour(novo.GT.Real)*60 + minute(novo.GT.Real))
    
27.09.2018 / 15:21