Home > Enterprise >  Finding Average of Times with HR:MIN in R
Finding Average of Times with HR:MIN in R

Time:06-28

I am trying to find the average time of the dataset below in R

c("1:00", "12:45", "12:45", "1:00", "7:30", "12:45", "7:15", 
"8:00", "12:30", "12:15", "12:45", "7:30", "7:45", "12:45", "11:30", 
"11:00", "10:45", "10:30", "4:30", "11:00", "11:00", "9:45", 
"9:45", "11:00", "11:15", "4:45", "11:15", "11:15", "11:00", 
"11:00", "2:15", "10:45", "10:45", "11:00", "11:30", "10:30", 
"11:00", "11:15", "10:45", "12:45", "1:15", "12:45", "7:45", 
"1:00", "1:15", "12:45", "1:00", "8:00", "1:15", "12:15", "11:00", 
"11:15", "5:00", "11:00", "10:45", "11:00", "11:30", "11:00", 
"5:00", "10:45", "11:00", "2:30", "10:45", "11:15", "11:15", 
"10:15", "11:00", "11:00", "11:00", "11:15", "11:15", "11:15", 
"11:00", "11:00", "2:30", "10:30", "10:30", "5:15", "10:15", 
"10:45", "11:15", "11:00", "11:15", "10:00", "12:30", "12:15", 
"12:45", "1:00", "8:15", "1:00", "7:30", "12:45", "12:30", "12:45", 
"12:30", "12:30", "1:00", "8:00", "12:15", "12:30")

Keeping in the same format

FWIW they are all currently 'character' class

This one particularly, with the answers provided produce 5:24

c("1:00", "1:45", "1:00", "12:45", "1:30", "1:00", "1:30", "1:45", 
"1:45", "12:45", "1:00", "12:45", "1:30", "1:15", "12:30", "12:30", 
"12:45", "1:00", "1:15", "1:15", "1:15", "12:30", "12:45", "1:15", 
"12:45", "1:00", "1:30", "1:15", "12:45", "12:45", "12:30")

CodePudding user response:

In base R you can use this:

format(mean(strptime(times, "%H:%M")), "%H:%M")

Output:

[1] "09:24"

CodePudding user response:

Here is a way.

  1. Coerce the character strings vector to package chron S3 class "time";
  2. compute the mean times with the method mean for this class;
  3. round to minutes;
  4. remove the seconds digits.
times <- c("1:00", "12:45", "12:45", "1:00", "7:30", "12:45", "7:15", 
           "8:00", "12:30", "12:15", "12:45", "7:30", "7:45", "12:45", "11:30", 
           "11:00", "10:45", "10:30", "4:30", "11:00", "11:00", "9:45", 
           "9:45", "11:00", "11:15", "4:45", "11:15", "11:15", "11:00", 
           "11:00", "2:15", "10:45", "10:45", "11:00", "11:30", "10:30", 
           "11:00", "11:15", "10:45", "12:45", "1:15", "12:45", "7:45", 
           "1:00", "1:15", "12:45", "1:00", "8:00", "1:15", "12:15", "11:00", 
           "11:15", "5:00", "11:00", "10:45", "11:00", "11:30", "11:00", 
           "5:00", "10:45", "11:00", "2:30", "10:45", "11:15", "11:15", 
           "10:15", "11:00", "11:00", "11:00", "11:15", "11:15", "11:15", 
           "11:00", "11:00", "2:30", "10:30", "10:30", "5:15", "10:15", 
           "10:45", "11:15", "11:00", "11:15", "10:00", "12:30", "12:15", 
           "12:45", "1:00", "8:15", "1:00", "7:30", "12:45", "12:30", "12:45", 
           "12:30", "12:30", "1:00", "8:00", "12:15", "12:30")

library(chron)

times <- as.times(paste0(times, ":00"))
mean(times)
#> [1] 09:24:27

mean_time <- round(mean(times), unit = "minutes")
mean_time
#> [1] 09:24:00

mean_time <- sub(":\\d{2}$", "",mean_time)
mean_time
#> [1] "09:24"

Created on 2022-06-27 by the reprex package (v2.0.1)

  • Related