[R] How to create a data.frame from several time series?
R. Michael Weylandt
michael.weylandt at gmail.com
Tue Apr 17 19:03:48 CEST 2012
It's a rather risky idea to call your function aggregate.zoo -- that's
actually a pre-existing function and it could (will!) confuse the
method dispatch system.
In short, S3 methods use the name convention generic.class (e.g.,
aggregate is the generic and zoo is the class) so when you just use
aggregate (a S3 generic function) it looks at the class of the object
and if it finds zoo, it will attempt to apply aggregate.zoo to it.
E.g.,
library(zoo)
z <- zoo(1:100, Sys.Date() + 1:100)
aggregate(z, as.Date(as.yearmon(time(z))), mean)
# Then I define your function
aggregate.zoo <- function(series) {
agg <- aggregate(data=series, value ~ month, ppk, lsl=1300, usl=1500)
return (zoo(x=agg$value, order.by=agg$month))
}
# Then I go away for a few days and try my same command
aggregate(z, as.Date(as.yearmon(time(z))), mean) # What just happened?!?!?!
Hope this helps,
Michael
On Tue, Apr 17, 2012 at 10:11 AM, Robert Latest <boblatest at gmail.com> wrote:
> Hello all,
>
> followup to yesterday's question: Part of my confusion was caused by
> my embarrassing mistake of overwriting the "ppk" function with another
> object, which of course broke the next iteration of the loop.
>
> Secondly, I got exactly what I wanted like this:
>
> aggregate.zoo <- function(series) {
> agg <- aggregate(data=series, value ~ month, ppk, lsl=1300, usl=1500)
> return (zoo(x=agg$value, order.by=agg$month))
> }
> l1 = split(df, df$tool)
> l2 = lapply(l1, aggregate.zoo)
> l3 = do.call(merge, l2)
>
> I puzzled this together from various example with only 80%
> understanding how it works and why.
>
> Regards,
> robert
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list