rtimechron

Calculate time difference between two hh:mm variables over the span of a day


I have an issue calculating the time difference between two variables. To be exactly the time between falling asleep and waking up. I thought about using chron for doing so and this is my example.

DF <- data.frame(time_start = c("20:00", "21:30", "22:00", "23:00", "00:30", "02:00", "04:00"),
                 time_end = c("03:00", "06:30", "07:00", "09:00", "5:30", "09:00", "10:00"))

library(chron)
DF$time_start <- paste0(DF$time_start, ":00")
DF$time_start <- chron(times. =DF$time_start)
DF$time_end <- paste0(DF$time_end, ":00")
DF$time_end <- chron(times. =DF$time_end)
DF$time_duration <- times(DF$time_end - DF$time_start)

Formatting the variables with chron works partially as intended. The summary for time_end looks good. time_start on the other hand is rather messy. And time_duration is decimal.

> summary(DF$time_start)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
00:30:00 03:00:00 20:00:00 13:17:09 21:45:00 23:00:00 
> summary(DF$time_end)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
03:00:00 06:00:00 07:00:00 07:08:34 09:00:00 10:00:00 
> summary(DF$time_duration)
      Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
-0.7083333 -0.6250000 -0.5833333 -0.2559524  0.2291667  0.2916667 

My questions:

How can I get plausible summary results for time_start and

how can I get the difference between time_start and time_end in a more readable way like hours? Thanks for your help in advance!


Solution

  • This gives the right durations in case that the end_time is the same or the next day as start_time:

    library(tidyverse)
    
    DF <- data.frame(time_start = c("20:00", "21:30", "22:00", "23:00", "00:30", "02:00", "04:00"),
                     time_end = c("03:00", "06:30", "07:00", "09:00", "5:30", "09:00", "10:00")) %>% 
      mutate_at(vars(starts_with("time")), hm) %>% 
      mutate(time_duration = if_else(
        time_end > time_start,
        time_end - time_start,
        hm("24:00") - time_start + time_end)
      )
    
    DF
      time_start  time_end time_duration
    1  20H 0M 0S  3H 0M 0S      7H 0M 0S
    2 21H 30M 0S 6H 30M 0S      9H 0M 0S
    3  22H 0M 0S  7H 0M 0S      9H 0M 0S
    4  23H 0M 0S  9H 0M 0S     10H 0M 0S
    5     30M 0S 5H 30M 0S      5H 0M 0S
    6   2H 0M 0S  9H 0M 0S      7H 0M 0S
    7   4H 0M 0S 10H 0M 0S      6H 0M 0S