如何在R中找到HH:MM(一天中的时间)的平均值?

时间:2018-06-23 20:54:34

标签: r excel time average mean

我的数据集记录了特定蝙蝠在不同夜晚的行为。我想找到这些时间的平均值,以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")

4 个答案:

答案 0 :(得分:4)

您可以使用lubridate在1行中完成此操作:

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

产生

[1] "11H 12M 0S"

答案 1 :(得分:1)

不是一个班轮(下面的代码经过调整,以解决OP在评论中提出的夜间vs.早晨问题):

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控制台中更易于阅读。

答案 2 :(得分:0)

您可以编写函数进行计算。唯一的问题是方法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发表评论之后,我编写了一个处理午夜后数小时的函数。

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 = ",")

答案 3 :(得分:0)

这里很少使用的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

...可以解释为午夜。