Search code examples
rlubridateperiod

calculate mean of period in lubridate


How to calculate mean of period in lubridate ?

library(lubridate)

# tibble
tbl <- tibble(time_1 = ymd_hms(c("2021-01-01 12:00:00"), ("2021-01-01 12:12:36"), ("2021-01-01 14:25:45")),
              time_2 = ymd_hms(c("2021-01-01 12:12:36"), ("2021-01-01 14:25:45"), ("2021-01-01 15:35:45")),
              time_period = seconds_to_period(difftime(time_2, time_1, units = 'sec')))

# calculate mean of period (WRONG ANSWER)
tbl %>% summarise(mean_val = mean(time_period))

Solution

  • The period object doesn't seem to work well with mean. You could use

    tbl %>%
      summarise(mean = seconds_to_period(mean(period_to_seconds(time_period))))
    

    to get

    # A tibble: 1 x 1
      mean      
      <Period>  
    1 1H 11M 55S
    

    To get another representation, you could try

    tbl %>%
      summarise(
        mean = seconds_to_period(mean(period_to_seconds(time_period))),
        mean_val = mean(period_to_seconds(time_period)),
        days     = mean_val %/% (60*60*24),
        hours    = mean_val %/% (60*60),
        minutes  = mean_val %/% 60 %% 60 + round((mean_val %% 60)/60),
        value    = paste0(days, "d ", hours, "H ", minutes, "M")
        )
    

    to get

    # A tibble: 1 x 6
      mean       mean_val  days hours minutes value    
      <Period>      <dbl> <dbl> <dbl>   <dbl> <chr>    
    1 1H 11M 55S     4315     0     1      12 0d 1H 12M
    

    I'm pretty sure, lubridate offers better tools for formating a Period object but I don't have much experience here.