Reputation: 369
I'm trying work with time data regarding the duration of several experiments. That data are in the form hh:mm:ss.
exp1 <- "34:03:07"
exp2 <- "00:01:10"
exp3 <- "01:13:41"
The first experiment is given as the total duration and the subsequent experiments are listed as time in excess of exp1. The total time result I'm looking for of exp2, for example, would then be 34:04:17 and for exp3 would be 35:16:48. What is the best method to add the times to exp1, when packages such as chron only work in hh:mm:ss up to 24:00:00?
Upvotes: 4
Views: 2039
Reputation: 173517
I often prefer to avoid Date/Time classes fur durations like this and will simply parse the string myself and convert them to a numeric value in minutes or seconds.
But as always, there's typically a package with similar functionality. In this case, lubridate has a duration
class that may be useful:
d <- new_duration(hour = 34,minute = 23,second = 23)
d1 <- new_duration(hour = 12,minute = 12,second = 23)
> d+d1
[1] 167746s (1.94d)
So you can do arithmetic with them and you can also create a duration object by passing numeric values (in seconds) to as.duration
.
Upvotes: 5