R 中 apply.daily() 中聚合的平均时间序列

Averaging time series in aggregate in apply.daily() in R

我有几个合并的每日动物园时间序列(假设合并集的名称是 'test'),它们以以下格式显示:

>test 


              TS1     TS2     TS3
2014-07-30    2.0     3.0     4.0
2014-07-31    2.5     3.0     4.5
2014-08-01    3.0     3.0     5.0

我想 aggregate/manipulate 以多种方式处理时间序列。然而,最简单的平均或求和正在逃避我。我尝试了以下方法:

ts <- apply.daily(as.xts(test),mean)

我原以为会得到以下输出:

>ts

                  X    
    2014-07-30    3.0     
    2014-07-31    3.3     
    2014-08-01    3.7    

但是,它 returns 与以前相同的时间序列。我知道这对我打算使用的 apply.weekly()apply.monthly() 很有用,但是我如何调整所有这些函数以将 TS1、TS2 和 TS3 包装成一个整体平均值相同的基础,同时保持 zoo/xts 格式。

非常感谢

根据显示的示例,我们可以连接行并取 mean

apply.daily(as.xts(test), function(x) round(mean(c(x)),1))
#          [,1]
#2014-07-30  3.0
#2014-07-31  3.3
#2014-08-01  3.7

请注意,在每日数据集上使用 OP 代码 returns 作为输入数据,因为标准只有一个观察值。假设如果数据集是日期时间 class,那么使用 apply.daily 将 return 每一天的 mean,用另一个 mean 将其包装起来以获得 mean 每行,即

test1 <- structure(list(TS1 = c(2, 2.5, 3, 2.2), TS2 = c(3, 3, 3, 3.2), 
TS3 = c(4, 4.5, 5, 4.4)), .Names = c("TS1", "TS2", "TS3"), 
 class = "data.frame", row.names = c("2014-07-30 07:00:00", 
"2014-07-31 05:00:00", "2014-08-01 03:00:00", "2014-07-30 07:20:00"))

apply.daily(as.xts(test1), function(x) round(mean(mean(x)),1))
#                   [,1]
#2014-07-30 07:20:00  3.1
#2014-07-31 05:00:00  3.3
#2014-08-01 03:00:00  3.7

因为我们使用的是匿名函数,所以不需要这两个mean

 apply.daily(as.xts(test1), function(x) round(mean(x),1))
 #                     [,1]
 #2014-07-30 07:20:00  3.1
 #2014-07-31 05:00:00  3.3
 #2014-08-01 03:00:00  3.7

用OP的方法检查上面的结果

apply.daily(as.xts(test1), mean)
#                   TS1 TS2 TS3
#2014-07-30 07:20:00 2.1 3.1 4.2
#2014-07-31 05:00:00 2.5 3.0 4.5
#2014-08-01 03:00:00 3.0 3.0 5.0

round(mean(c(2.1, 3.1, 4.2)), 1)
#[1] 3.1
round(mean(c(2.5, 3.0, 4.5)), 1)
#[1] 3.3