Work with durations over 24 hours in R
Asked Answered
T

1

6

I have a series of duration that range up to 118 hours in a format like so "118:34:42" where 118 is hours, 34 is minutes, and 42 is seconds. Output should be a number of seconds.

I would like to convert this to some kind of time type in R, but most of the libraries I've looked at want to add a date (lubridate, zoo, xts), or return "NA" due to the hours being beyond a 24 hour range. I could parse the string and return a number of seconds, but I'm wondering if there's a faster way.

I'm slightly new to R (maybe 3 months in to working with this).

Any help figuring out how to deal with this would be appreciated.

Example:

    library(lubridate)
    x <- c("118:34:42", "114:12:12")
    tt <- hms(x)
    Error in parse_date_time(hms, orders, truncated = truncated, quiet = TRUE) : 
  No formats could be infered from the training set.
    #try another route
    w <- "118:34:42"
    tt2 <- hms(w)
    tt2
    #[1] NA
    z <- "7:02:02"
    tt3 <- hmw(z)
    tt3
    #[1] "7H 2M 2S"
Teach answered 21/12, 2012 at 6:14 Comment(1)
You could convert the times to seconds: library(gsubfn); secs <- c(strapply(x, "\\d+", as.numeric) %*% c(3600, 60, 1)) and then do all your processing in seconds. To convert back: sprintf("%d:%02d:%02d", secs %/% 3600, secs %/% 60 %% 60, secs %% 60)Brandwein
C
7

In the lubridate package there is a function hms() that returns a time object:

library(lubridate)

x <- c("118:34:42", "114:12:12")
tt <- hms(x)

tt
[1] 118 hours, 34 minutes and 42 seconds 
[2] 114 hours, 12 minutes and 12 seconds 

The function hms() returns an object of class Period:

str(tt)
Formal class 'Period' [package "lubridate"] with 6 slots
  ..@ .Data : num [1:2] 42 12
  ..@ year  : num [1:2] 0 0
  ..@ month : num [1:2] 0 0
  ..@ day   : num [1:2] 0 0
  ..@ hour  : num [1:2] 118 114
  ..@ minute: num [1:2] 34 12

You can do arithmetic using these objects. For example:

tt[2] - tt[1]
[1] -4 hours, -22 minutes and -30 seconds 
Clincher answered 21/12, 2012 at 7:33 Comment(5)
I still get NA's for the above. Here's the output I'm receiving: 'code' tt <- hms(timecolumn) > head(timecolumn) [1] "115:53:34" "7:13:27" "12:12:55" "3:28:35" "3:05:23" "0:10:30" > head(tt) [1] NA "7H 13M 27S" "12H 12M 55S" "3H 28M 35S" "3H 5M 23S" "10M 30S" 'code'Teach
@Teach To trace this you'll have to give much more information about your setup, e.g. version of R, version of Lubridate, etc.Clincher
Running Lubridate 1.20, R 2.15.2. I ran a few updates this morning just to check if any packages were out of date. Trying to debug the issue now. Going to walk through a few and see if I can find anything from the Lubridate help. Thanks for the pointers so far.Teach
Try to construct a minimally reproducible example and post it into your question, then I can also take a look.Clincher
@Andrie, I think you are using an old version of lubridate as I get NAs on anything greater than hms("24:00:00") using R 2.15.2 on Vista with both lubridate 1.2.0 and also with 1.2.1 from github. With lubridate 0.2.5 on R 2.13 I do get non-NA results so it only seems to work in sufficiently old versions.Brandwein

© 2022 - 2024 — McMap. All rights reserved.