R 聚合平均值";%H%M“;在;“一周”;垃圾桶

R 聚合平均值";%H%M“;在;“一周”;垃圾桶,r,aggregate-functions,aggregate,time-series,R,Aggregate Functions,Aggregate,Time Series,我已经为此挣扎了一段时间。我不熟悉ts数据和所有相关的R包。 我有一个带有多个变量的df,包括格林尼治标准时间“%H%M”中的“时间”和采样发生的日期“%Y/%M/%e”。我想将我的日期数据按“周”(即%W/%g)进行分类/汇总,并计算在该周内进行采样时的平均“一天中的时间” 通过首先将df转换为zoo对象,然后使用aggregate.zoo命令,我可以计算数值变量(例如,权重)的其他乐趣,如下所示: #calculate the sum weight captured every week

我已经为此挣扎了一段时间。我不熟悉ts数据和所有相关的R包。 我有一个带有多个变量的df,包括格林尼治标准时间“%H%M”中的“时间”和采样发生的日期“%Y/%M/%e”。我想将我的日期数据按“周”(即%W/%g)进行分类/汇总,并计算在该周内进行采样时的平均“一天中的时间”

通过首先将df转换为zoo对象,然后使用aggregate.zoo命令,我可以计算数值变量(例如,权重)的其他乐趣,如下所示:

#calculate the sum weight captured every week 
x2c <- aggregate(OA_zoo, as.Date(cut(time(OA_zoo), "week")), sum)
我会这样处理: 首先,使用表示周的字符串创建一列:

godin$week <- format(godin$date2, "%Y-W%U")
注意:以上是一个小技巧…
strtime()
假设当前日期(如果未指定),但这不应妨碍此特定应用程序,因为所有转换的时间将具有相同的日期,平均值的时间部分将是正确的。我稍后会取消约会

在这一点上,我认为您可以简单地汇总:

x2c <- aggregate(time2~week, data=godin, FUN=mean)

这里的教训是,在R中推来推去没有相关日期的时间是很棘手的。我希望听到其他人有更好的方法来做这件事。

如果你想要每周所有观察时间的平均值,并且希望日期重要(即今天19:00和明天19:00将平均到明天早上07:00),那你就可以这么做了

godin$datetime <- as.POSIXct(paste(godin$date2, godin$TIME), format="%Y-%m-%d %H%M")
aggregate(godin$datetime, list(format(godin$datetime, "%W/%g")), mean)

#  Group.1                   x
#1   28/04 2004-07-18 09:40:00
#2   30/04 2004-07-31 01:45:00
#3   31/05 2005-08-02 00:27:30
#4   36/04 2004-09-10 13:51:15
#5   36/05 2005-09-11 00:26:40
#6   37/05 2005-09-13 00:44:10
编辑2

如果您只想返回格式化为
%H%M

out <- aggregate(godin$stime, list(format(godin$datetime, "%W/%g")), function(TIME) format(mean(TIME), "%H%M"))
out[order(as.numeric(paste0(substr(out[, 1], 4, 5), substr(out[, 1], 1, 2)))), ]
#  Group.1    x
#1   28/04 0940
#2   30/04 0945
#4   36/04 1151
#3   31/05 1227
#5   36/05 1226
#6   37/05 1244

out您需要将
时间
列转换为常用单位(即分钟)。以下是几个帮助器函数:

hour2min <- function(hhmm) {
  hhmm <- as.numeric(hhmm)
  trunc(hhmm/100)*60 + hhmm %% 100
}
min2hour <- function(min) {
  min <- as.numeric(min)
  trunc(min/60)*100 + min %% 60
}

秒仍在100分之一分钟内,但可以更改…

我不清楚这个问题。计算一个时间的平均值(与数字相反)或按周聚合有问题吗?你真的需要为任何人提供帮助。你如何计算一天时间的平均值?你不会改做中位数还是模式?@JoshuaUlrich:bah。你说得对。如何转换没有日期的时间?改为使用
strtime()
编辑。还是有点麻烦,请看答案,如果可以的话请帮助,谢谢
strtime
返回
POSIXlt
,这是一个列表,所以这不起作用。将
包装为.POSIXct
至少可以避免错误,但我认为结果没有意义。@GSee:谢谢!我在测试的过程中艰难地发现了这一点。由于特定的应用程序,结果中的“无意义”日期部分不会成为障碍。我最新编辑的回复经过测试,终于生效了@戈迪娜,别让我卑鄙的回答愚弄你;如果我亲自这么做,我会在做任何事情之前将我的数据转换为
xts
。好奇地想知道,为什么你的结果与@mac不同?因为我像你问的那样使用了
%W/%g
;-)。请注意,例如,@mac将2004-07-28视为与2004-08-01不同的一周,如果您替换
godin$week,谢谢您的回答@Joshua Ulrich,但这并不是我想要的。见下面的答案。
> x2c
      week    time2
1 2004-W29 09:40:00
2 2004-W30 01:45:00
3 2004-W31 13:45:00
4 2004-W36 12:07:00
5 2004-W37 10:32:30
6 2005-W31 12:27:30
7 2005-W36 10:48:20
8 2005-W37 13:11:06
godin$datetime <- as.POSIXct(paste(godin$date2, godin$TIME), format="%Y-%m-%d %H%M")
aggregate(godin$datetime, list(format(godin$datetime, "%W/%g")), mean)

#  Group.1                   x
#1   28/04 2004-07-18 09:40:00
#2   30/04 2004-07-31 01:45:00
#3   31/05 2005-08-02 00:27:30
#4   36/04 2004-09-10 13:51:15
#5   36/05 2005-09-11 00:26:40
#6   37/05 2005-09-13 00:44:10
godin$stime <- as.POSIXct(paste("1970-01-01", godin$TIME), format='%Y-%m-%d %H%M')
aggregate(godin$stime, list(format(godin$datetime, "%W/%g")), mean)

#  Group.1                   x
#1   28/04 1970-01-01 09:40:00
#2   30/04 1970-01-01 09:45:00
#3   31/05 1970-01-01 12:27:30
#4   36/04 1970-01-01 11:51:15
#5   36/05 1970-01-01 12:26:40
#6   37/05 1970-01-01 12:44:10
out <- aggregate(godin$stime, list(format(godin$datetime, "%W/%g")), mean)
out[order(as.numeric(paste0(substr(out[, 1], 4, 5), substr(out[, 1], 1, 2)))), ]
#  Group.1                   x
#1   28/04 1970-01-01 09:40:00
#2   30/04 1970-01-01 09:45:00
#4   36/04 1970-01-01 11:51:15
#3   31/05 1970-01-01 12:27:30
#5   36/05 1970-01-01 12:26:40
#6   37/05 1970-01-01 12:44:10
out <- aggregate(godin$stime, list(format(godin$datetime, "%W/%g")), function(TIME) format(mean(TIME), "%H%M"))
out[order(as.numeric(paste0(substr(out[, 1], 4, 5), substr(out[, 1], 1, 2)))), ]
#  Group.1    x
#1   28/04 0940
#2   30/04 0945
#4   36/04 1151
#3   31/05 1227
#5   36/05 1226
#6   37/05 1244
hour2min <- function(hhmm) {
  hhmm <- as.numeric(hhmm)
  trunc(hhmm/100)*60 + hhmm %% 100
}
min2hour <- function(min) {
  min <- as.numeric(min)
  trunc(min/60)*100 + min %% 60
}
library(xts)
x <- xts(hour2min(godin$TIME), as.Date(godin$date2), dimnames=list(NULL,"MINS"))
w <- apply.weekly(x, mean)
w$TIME <- min2hour(w$MINS)
#                MINS     TIME
# 2004-07-18 580.0000  940.000
# 2004-08-01 585.0000  945.000
# 2004-09-12 711.2500 1151.250
# 2005-08-02 747.5000 1227.500
# 2005-09-11 746.6667 1226.667
# 2005-09-13 764.1667 1244.167