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

前端 未结 3 528
悲&欢浪女
悲&欢浪女 2021-01-21 04:27

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:



        
相关标签:
3条回答
  • 2021-01-21 04:35

    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"
    
    0 讨论(0)
  • 2021-01-21 04:39

    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
    
    0 讨论(0)
  • 2021-01-21 04:50

    We can do this without using any external packages

    format(mean(strptime(Col_Time, "%H:%M:%S")), "%H:%M:%S")
    #[1] "03:17:26"
    

    data

    Col_Time = c('03:08:20','03:11:30','03:22:18','03:27:39')
    
    0 讨论(0)
提交回复
热议问题