Work with a duration of more than 24 hours in R

I have a series up to 118 hours in the format "118: 34: 42", where 118 hours, 34 minutes, and 42 seconds. The output should be in a few seconds.

I would like to convert this to some type of time in R, but most of the libraries I looked at want to add a date (lubridate, zoo, xts) or return β€œNA” because of the clock, being outside the 24-hour range. I can parse the string and return a few seconds, but I'm wondering if there is a faster way.

I'm a little new to R (maybe after 3 months of working with this).

Any help in figuring out how to handle this would be greatly 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" 
+4
source share
1 answer

The lubridate package has an hms() function 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 hms() function returns an object of the Period class:

 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 perform arithmetic using these objects. For instance:

 tt[2] - tt[1] [1] -4 hours, -22 minutes and -30 seconds 
+5
source

All Articles