Search code examples
rdatetimechron

How do I do operations with hours greater than 24


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

Solution

  • 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