Dleightful
Dleightful

Reputation: 107

How to convert time in H:M:S to decimal minutes?

I have a table that has the following set of numbers:

Time in Hours Used
  1:05:34
  1:04:45
  1:12:04
    34:17
    43:35

... for fifty five observations

How can I create a new column with the time in hours being listed as minutes. I know I can multiply it by 24*60 but for some reason when I do this, it says that factors can't be multiplied.

I have tried other programs like chronos but they would require me to literally input every single value and create a vector of values for all 55 observations. How can I just multiply each one of these values by 24*60 to get them in minute?

Upvotes: 1

Views: 999

Answers (1)

akrun
akrun

Reputation: 887851

We can use guess_formats from lubridate to get the multiple formats in the dataset column, use that in parse_date_time to convert to date time class. We extract the time part (%H:%M:%S) with format, convert to times class using times from chron, and then we multiply with 24*60 after converting to numeric.

library(chron)
library(lubridate)
as.numeric(times(format(parse_date_time(df1[,1],guess_formats(df1[,1], 
                  c('hms', 'ms'))), '%H:%M:%S')))*24*60

data

df1 <- structure(list(timeinhours = structure(c(2L, 1L, 3L, 4L, 5L),
.Label = c("1:04:45", "1:05:34", "1:12:04", "34:17", "43:35"),
class = "factor")), .Names = "timeinhours", row.names = c(NA, -5L),
class = "data.frame")

Upvotes: 1

Related Questions