为什么使用 xts/zoo 在 R 中没有 apply.hourly? [英] Why is there no apply.hourly in R with xts/zoo?
本文介绍了为什么使用 xts/zoo 在 R 中没有 apply.hourly?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按小时平均汇总数据.日常很简单:
I want to aggregate data by hourly mean. Daily is very easy:
apply.daily(X2,mean)
为什么没有每小时的功能?我试过了
Why is there no function for hourly? I tried
hr.means <- aggregate(X2, format(X2["timestamp"],"%Y-%m-%d %H"))
并且在使用 trim 参数时总是出错.有没有类似apply.daily的更简单的功能?如果我想汇总 5 分钟的平均值怎么办.数据是每分钟的值:
and got always error with trim argument. Is there an easier function similar to apply.daily? What if I want to aggregate the mean of 5 minutes. Data are values per minute:
"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.
I am using xts and zoo.
推荐答案
试试
period.apply(X2, endpoints(X2, "hours"), mean)
apply.daily
只是上述内容的包装器:
apply.daily
is simply a wrapper for the above:
> apply.daily
function (x, FUN, ...)
{
ep <- endpoints(x, "days")
period.apply(x, ep, FUN, ...)
}
这篇关于为什么使用 xts/zoo 在 R 中没有 apply.hourly?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文