Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Adding durations in HMS, ideally with lubridate R

$
0
0

My apologies if it is a repeat question. I'm not experienced working with timestamp data in R.

I have a dataset that contains a duration data in HMS format. I want to find the total sum of these values.

library(lubridate)
name <- c("one", "one", "two", "two") 
duration <- lubridate::hms("38H 3M 24S", "6H 50M 58S", "31M 54S", "8H 13M 51S") 
data <- data.frame(name , duration, stringsAsFactors=FALSE)

If I add elements directly I get something that does not account for the carrying over of time on the 60.

d1 <- data$duration[1] + data$duration[2]
d1

This returns "44H 53M 82S" - 82 seconds should be 1M 22S

Similar issues with the sum function

d2 <- sum(data$duration)
d2

this returns 82.

I'm looking down the path of converting hms to seconds, summing those values and converting back to HMS and I thought surely someone has had to add HMS together before?

My end goal is to be able to do something like this

d4 <- data %>%
   group_by(name) %>%
   summarise(totalTime = sum(duration)) 

Thanks in advance for the comments.


Viewing all articles
Browse latest Browse all 201867

Trending Articles