How do I do operations with hours greater than 24

341 views Asked by At

I have a list of how much time students have spent in a platform. The platform sends me that data as cumulative time meaning that I have times greater than 24h. I have been using the Chron package to sort and do operations with this times and I just realized that it doesn't accept time values greater than 24. I could transform this to seconds but I'd like to maintain the HH% MM% S% display format.

> time <- chron(times = "24:55:00")
Warning message:
In convert.times(times., fmt) :
time-of-day entries out of range in positions 1 set to NA
> time
Time in days:
[1] NA
2

There are 2 answers

0
pogibas On BEST ANSWER

I strongly recommend to use lubridate from tidyverse when working with dates.

library(lubridate)
foo <- "24:55:00"

bar <- hms(foo)
bar
[1] "24H 55M 0S"

class(bar)
[1] "Period"
attr(,"package")
[1] "lubridate"

# Time in days
bar / hours(24)
[1] 1.038194
0
IceCreamToucan On

You can use the hms function in the lubridate package instead.

hms("24:55:00") - hms("26:45:00")
## [1] "-2H 10M 0S"