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"))
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
I am using xts and zoo.
eXtensible Time Series (xts) is a powerful package that provides an extensible time series class, enabling uniform handling of many R time series classes by extending zoo.
xts objects are simple. Think of them as a matrix of observations combined with an index of corresponding dates and times. xts = matrix + times. The main xts constructor takes a number of arguments, but the two most important are x for the data and order.by for the index. x must be a vector or matrix.
try
period.apply(X2, endpoints(X2, "hours"), mean)
apply.daily
is simply a wrapper for the above:
> apply.daily
function (x, FUN, ...)
{
ep <- endpoints(x, "days")
period.apply(x, ep, FUN, ...)
}
hr.means <- aggregate(X2, format(time(X2),"%y-%m-%d %H"), mean)
This should work fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With