Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

average time in a column in hr:min:sec format

Tags:

r

I am trying to identify the average time in a column of a data frame in hr:min:sec format using R. But no luck. Help is much appreciated. Data sample is as below:

Col_Time
03:08:20
03:11:30
03:22:18
03:27:39

My output should be one record which is average of all the numbers in same format like the input column.

Thanks

like image 668
Jahnab Kumar Deka Avatar asked Dec 11 '22 12:12

Jahnab Kumar Deka


2 Answers

You can use chron library. Specifically, times function. Note that times internally represents time as a numeric value (decimal days).

Col_Time = c('03:08:20','03:11:30','03:22:18','03:27:39')
library(chron)
mean(times(Col_Time))
#[1] 03:17:27
like image 122
d.b Avatar answered Jan 08 '23 06:01

d.b


One way with lubridate package. We convert the time into seconds, take the mean of it and then convert those seconds to time again.

Col_Time = c('03:08:20','03:11:30','03:22:18','03:27:39')
library(lubridate)
seconds_to_period(mean(period_to_seconds(hms(Col_Time))))
#[1] "3H 17M 26.75S"
like image 31
Ronak Shah Avatar answered Jan 08 '23 05:01

Ronak Shah