Convert number to minute and subtract those minutes from a column containing date and time R

Asked

Viewed 160 times

-3

Good morning, I have a column (minutes1) that has data of the amount of min (105,90,80)

I need to turn those minutes into hours (HH:MM), after which subtract these created hours from another start column (DD/MM/YYYYYY HH:MM) that has data in date and time and minute ("2018-06-01 12:00", "2018-06-01 11:00", "2018-06-01 15:20")

Thank you very much in advance.

  • @Marcusnunes I understand that now he is wanting the opposite. On this question that you linked, he would like to turn hours into minutes and now he wants to turn minutes into hours and then do some operations.

  • Luis I recommend that you, when creating a question here on the site, put your database, or a part of it so that we can help you easier. Search about the command dput in the R

  • I understand your point, but wouldn’t one question be the other? I imagine that the AP can interpret the code provided in the link I put and perform the reverse operation if tried. Note that the question even has an attempt to solve.

  • 1

    @Marcusnunes you are covered with reason. It is easier to create a new question than to dwell on attempts

  • Good afternoon guys, sorry the delay in responding, What happens I have a column Tempototal = c("15/08/2018 06:00", "16/08/2018 10:35") solo = c("01:00", "02:00") and start.de.use that has to be c("15/08/2018 05:00", "16/08/2018 08:35") So I asked the first question, now I have to convert the minutes into hours, subtract those minutes from the tempototal column, and create a new column with the date field and the time of this subtraction.

1 answer

1


I created a database with the example of the variables you reported to facilitate.

dados <- data.frame(
  inicio = c("2018-06-01 12:00", "2018-06-01 11:00", "2018-06-01 15:20"),
  minuto1 = c(105, 90, 80) )

I used the package chron

library(chron)

#criação da variável horas_inicias, extraindo essa informação de inicio
dados$horas_inicio <- as.character(substr(dados$inicio, 12, 16))
#acréscimo de segundos para utilizar a função times
dados$horas_inicio <- paste0(dados$horas_inicio, ":00")
#transformação da variável para tempo
dados$horas_inicio <- times(dados$horas_inicio)
#criação da variável horas, transformando minutos
dados$horas <- times((dados$minuto1%/%60 +  dados$minuto1%%60 /60)/24)
#cálculo da diferença de tempo entre as variáveis criadas
dados$diferença <- dados$horas_inicio - dados$horas
  • Rafael thanks so much again for the help.

Browser other questions tagged

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