Handling time data - chronopsychiatry/AmbientViewer GitHub Wiki

Computing average times

mean_time computes the average time from a vector of times in ISO8601 format. It returns the average time (single value) in format HH:MM.

time_vector <- c("2025-04-08 23:00:00", "2025-04-09 01:00:00")
mean_time(time_vector)

#> "00:00"

Computing min and max times

When looking for example at sleep onset or wakeup times, it can be useful to compute the earliest (min) or latest (max) times. Since we typically want to know the earliest or latest time per night, 23:00 should be considered earlier than 01:00.

The min_time and max_time functions compute the earliest and latest time over a window ranging from 12pm to 12pm.

⚠ This means that 13:00 will be considered earlier than 11:00.

time_vector <- c("2025-04-08 23:00:00", "2025-04-09 01:00:00", "2025-04-10 19:00:00", "2025-04-11 03:00:00")
min_time(time_vector)

#> "19:00"
time_vector <- c("2025-04-08 23:00:00", "2025-04-09 01:00:00", "2025-04-10 19:00:00", "2025-04-11 03:00:00")
max_time(time_vector)

#> "03:00"