Demo
Demo

Reputation: 301

Convert factor type time into number of minutes

There is a column in my dataset that contains time in the format 00:20:10. I have two questions. First, when I import it into R using read.xlsx2(), this column is converted to factor type. How can I convert it to time type? Second, I want to calculate each person's total time in number of minutes.

ID      Time
 1  00:10:00
 1  00:21:30
 2  00:30:10
 2  00:04:10

The output I want is:

ID Total.time
 1       31.5
 2       34.3

I haven't dealt with time issue before, and I hope someone would recommend some packages as well.

Upvotes: 2

Views: 944

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99391

You could use times() from the chron package to convert the Time column to "times" class. Then aggregate() to sum the times, grouped by the ID column. This first block will give us actual times in the result.

library(chron)

df$Time <- times(df$Time)
aggregate(list(Total.Time = df$Time), df[1], sum)
#   ID Total.Time
# 1  1   00:31:30
# 2  2   00:34:20

For decimal output, we can employ minutes() and seconds(), also from chron.

aggregate(list(Total.Time = df$Time), df[1], function(x) {
    minutes(s <- sum(x)) + (seconds(s) / 60)
})
#   ID Total.Time
# 1  1   31.50000
# 2  2   34.33333

Furthermore, we can also use data.table for improved efficiency.

library(data.table)
setDT(df)[, .(Total.Time = minutes(s <- sum(Time)) + (seconds(s) / 60)), by = ID]
#    ID Total.Time
# 1:  1   31.50000
# 2:  2   34.33333

Data:

df <- structure(list(ID = c(1L, 1L, 2L, 2L), Time = structure(c(2L, 
3L, 4L, 1L), .Label = c("00:04:10", "00:10:00", "00:21:30", "00:30:10"
), class = "factor")), .Names = c("ID", "Time"), class = "data.frame", row.names = c(NA, 
-4L))

Upvotes: 5

Related Questions