How to turn a given 1:30 hour into 90 minutes in R

Asked

Viewed 321 times

-4

I have a column with data of 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")
  • 1

    Multiply the hour by 60 and add to the minutes

  • 1

    Your question was not very well constructed. Next time, enter the code of what you have tried

1 answer

3


Using the following vector as an example

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

Follow 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 functions times and hms transform the object horas in a time format.

To add these values as a new variable from 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 package dplyr - I will illustrate with several mutate for you to understand step by step. You could put everything within 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))
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.