为什么在使用xts / zoo的R中没有apply.hourly?

时间:2013-04-15 15:35:08

标签: r time-series xts zoo

我希望按小时平均汇总数据。每日很容易:

apply.daily(X2,mean)

为什么每小时没有功能? 我试过了

hr.means <- aggregate(X2, format(X2["timestamp"],"%Y-%m-%d %H"))

并且修剪参数总是出错。 是否有类似apply.daily的更简单的功能?如果我想聚合5分钟的平均值怎么办? 数据是每分钟的值:

"timestamp", value 
"2012-04-09 05:03:00",2
"2012-04-09 05:04:00",4
"2012-04-09 05:05:00",5
"2012-04-09 05:06:00",0
"2012-04-09 05:07:00",0
"2012-04-09 05:08:00",3
"2012-04-09 05:09:00",0
"2012-04-09 05:10:00",1

我正在使用xts和zoo。

3 个答案:

答案 0 :(得分:15)

period.apply(X2, endpoints(X2, "hours"), mean)

apply.daily只是上面的包装器:

> apply.daily
function (x, FUN, ...)
{
    ep <- endpoints(x, "days")
    period.apply(x, ep, FUN, ...)
}

答案 1 :(得分:2)

hr.means <- aggregate(X2, format(time(X2),"%y-%m-%d %H"), mean) 

这应该可以正常工作。

答案 2 :(得分:0)

Answering part 2:

What if I want to aggregate the mean of 5 minutes?

As @eddit already mentioned in a comment above:

df <- read.table(header=TRUE, sep=",", stringsAsFactors=FALSE, text="
timestamp, value 
2012-04-09 05:03:00,2
2012-04-09 05:04:00,4
2012-04-09 05:05:00,5
2012-04-09 05:06:00,0
2012-04-09 05:07:00,0
2012-04-09 05:08:00,3
2012-04-09 05:09:00,0
2012-04-09 05:10:00,1")
X2 <- xts(df$value, as.POSIXct(df$timestamp))

X2.5min <- period.apply(X2, endpoints(X2, "minutes", 5), mean)

I get: 05:04:00 - 4; 05:09:00 - 5,... but maybe it is possible to set the first value to 05:00:00 and go on with 05:05:00 might be easier, if I am merging files later to have the same start and timestep.

Indeed:

> X2.5min
                    [,1]
2012-04-09 05:04:00  3.0
2012-04-09 05:09:00  1.6
2012-04-09 05:10:00  1.0

Darren Cook over at Cross Validated faced the same issue and wrote function align.time.down:

align.time.down=function(x,n){index(x)=index(x)-n;align.time(x,n)}

That can be used to adjust the times down:

X2.5mindown <- align.time.down(X2.5min, 5 * 60)
X2.5mindown
                    [,1]
2012-04-09 05:00:00  3.0
2012-04-09 05:05:00  1.6
2012-04-09 05:10:00  1.0