【问题标题】:How to find Averages of HH:MM (time of day) in R? [closed]如何在R中找到HH:MM(一天中的时间)的平均值? [关闭]
【发布时间】:2018-12-02 22:28:44
【问题描述】:

我的数据集记录了某些蝙蝠在不同夜晚的行为。我想找到这些时间的平均值,这些时间采用 hh: mm(24 小时制)格式。

Onset:
23:42,
21:40,
21:20,
21:30,
22:15,
23:40,
23:30,
02:10,
00:40,
01:35,
01:28,
01:00,
01:00,
00:55,
01:35.

对于可能的R解决方案,样本数据:

onset <- c("23:42","21:40","21:20","21:30","22:15","23:40","23:30",
           "02:10","00:40","01:35","01:28","01:00","01:00","00:55","01:35")

【问题讨论】:

    标签: r excel time average mean


    【解决方案1】:

    您可以在 1 行中使用 lubridate

    seconds_to_period(mean(period_to_seconds(hm(Onset))))
    

    产生

    [1] "11H 12M 0S"
    

    【讨论】:

      【解决方案2】:

      不是一个班轮(以下代码已调整以解决 OP 在 cmets 中提出的夜间与早晨问题):

      onset <- c("23:42","21:40","21:20","21:30","22:15","23:40","23:30",
                 "02:10","00:40","01:35","01:28","01:00","01:00","00:55","01:35")
      
      library(tibble)
      
      onset.df <- t(data.frame(strsplit(onset, ":"), stringsAsFactors=F))
      colnames(onset.df) <- c("hours", "minutes")
      onset.df <- as_tibble(onset.df)
      onset.df$hours <- as.numeric(onset.df$hours)
      onset.df$minutes <- as.numeric(onset.df$minutes)
      onset.df$minutes.fraction <- onset.df$minutes/60
      onset.df$hours.fraction <- onset.df$hours+onset.df$minutes.fraction
      mean(onset.df$hours.fraction)
      [1] 11.2
      
      # alternative approach to account for night / morning
      onset.df$hours <- ifelse(onset.df$hours < 12, 
                               onset.df$hours+24, onset.df$hours)
      onset.df$hours.fraction <- onset.df$hours+onset.df$minutes.fraction
      onset.df
      # A tibble: 15 x 4
         hours minutes minutes.fraction hours.fraction
         <dbl  <dbl           <dbl         <dbl>
       1  23.0    42.0            0.700           23.7
       2  21.0    40.0            0.667           21.7
       3  21.0    20.0            0.333           21.3
       4  21.0    30.0            0.500           21.5
       5  22.0    15.0            0.250           22.2
       6  23.0    40.0            0.667           23.7
       7  23.0    30.0            0.500           23.5
       8  26.0    10.0            0.167           26.2
       9  24.0    40.0            0.667           24.7
      10  25.0    35.0            0.583           25.6
      11  25.0    28.0            0.467           25.5
      12  25.0     0              0               25.0
      13  25.0     0              0               25.0
      14  24.0    55.0            0.917           24.9
      15  25.0    35.0            0.583           25.6
      onset.mean.raw <- mean(onset.df$hours.fraction)
      onset.mean.format <- ifelse(onset.mean.raw >= 24, 
                                  onset.mean.raw-24, onset.mean.raw)
      onset.mean.format.hour <- round(onset.mean.format, 0)
      onset.mean.format.minutes <- round((onset.mean.format-onset.mean.format.hour)*60, 0)
      paste("Average time of onset:", onset.mean.format.hour, "hours and",
             onset.mean.format.minutes, "minutes")
      [1] "Average time of onset: 0 hours and 0 minutes"
      

      我只是使用tibble 来去掉行名并使表格在R 控制台中更易于阅读。

      【讨论】:

      • 平均时间“11.2”或“11:12”在逻辑上是早上的时间点。这是一种误导,因为所有记录的时间都是在 21:00 到 03:00 之间的夜晚。这个 11:12 可以被认为是 23:12 吗?如果是,那么数学哪里出错了?
      • @BaheerathanM 查看修改后的答案。如果小时小于 12,您只需添加 24 即可。的,如果你得到一个像25这样的答案,它可以理解为早上1。
      • @BaheerathanM 答案再次更新。
      【解决方案3】:

      很少使用的as.difftime 在这里会很方便,以匹配@Hack-R 的结果:

      mean(as.difftime(dat$Onset, format="%H:%M", units="hours"))
      #Time difference of 11.2 hours
      

      不过,由于时间已经过去,我认为你需要变得更狡猾:

      out <- as.numeric(as.difftime(dat$Onset, format="%H:%M", units="hours"))
      mean(ifelse(out < 12, out + 24, out))
      # [1] 24
      

      ...可以解释为午夜。

      【讨论】:

        【解决方案4】:

        您可以编写一个函数来进行计算。唯一的问题是方法mean.POSIXct 需要它的参数属于"POSIXct""POSIXt" 类。由于您只有 HH:MM,因此该函数会为您执行强制转换。

        meanHour <- function(h, format = "%H:%M"){
          hh <- as.POSIXct(paste(Sys.Date(), h), "%Y-%m-%d %H:%M")
          hmean <- mean(hh)
          format(hmean, format = format)
        }
        
        meanHour(Onset)
        #[1] "11:12"
        

        编辑。

        在 OP 的 cmets 之后,我编写了一个处理午夜过后几个小时的函数。

        meanHour2 <- function(h){
          f <- function(x){
            x[1] <- ifelse(x[1] < 12, x[1] + 24, x[1])
            60*x[1] + x[2]
          }
        
          hh <- strsplit(h, ":")
          hh <- lapply(hh, as.integer)
          hh <- sapply(hh, f)
          hmean <- mean(hh)
          H <- hmean %/% 60
          M <- hmean %% 60
          sprintf("%02d:%02d", H, M)
        }
        
        meanHour2(h)
        #[1] "24:00"
        

        数据。

        Onset <- scan(what = character(), 
                  text = "23:42, 21:40, 21:20, 21:30, 22:15, 23:40, 
                          23:30, 02:10, 00:40, 01:35, 01:28, 01:00, 
                          01:00, 00:55, 01:35",
                  sep = ",")
        

        【讨论】:

        • 平均时间“11:12”在逻辑上是早上的时间点。这是一种误导,因为所有记录的时间都是在 21:00 到 03:00 之间的夜晚。这个 11:12 可以被认为是 23:12 吗?如果是,那么数学哪里出错了?
        • (21:00 + 03:00)/2 == 24:00/2 == 12:00。我也注意到了这一点,但后来我看到了午夜过后的时间。结果是上午 11:12。
        猜你喜欢
        • 1970-01-01
        • 2019-03-08
        • 2019-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-12
        • 2015-07-03
        • 1970-01-01
        相关资源
        最近更新 更多