Search code examples
rdplyrposixct

Given a sequence of POSIXct which include date and time how do I calculate the mean of times only (excluding days)?


start =  as.POSIXct("2019-04-12 00:00:00", tz = "UTC")

end = as.POSIXct("2019-07-14 09:00:00", tz = "UTC")

datseq = data.frame(time_factor = seq(start,end, by = "1 hour"))

sampledat = sample(datseq$time_factor,30,replace = FALSE)

Given the above data, if I were to calculate the mean mean(sampledat) it will return the mean time of hours and days. But how can I extract only the time and calculate the mean of times only?

Thanks!


Solution

  • Here is one way using some help from lubridate library.

    library(lubridate)
    
    format(sampledat, '%T') %>%
      hms %>%
      period_to_seconds() %>%
      mean %>%
      as.POSIXct(origin = '1970-01-01', tz = 'UTC') %>%
      format('%T')
    

    We extract the time part from sampledat, convert it to period object, convert that into number of seconds, take mean of it and display it in "HMS" format.