[R] Getting Annual (Conditional) Averages

Gabor Grothendieck ggrothendieck at gmail.com
Sat Nov 17 14:34:44 CET 2007


Assuming we use zoo, we define a test series, z, and then aggregate it:

> library(zoo)
> z <- zooreg(1000:2000, start = as.Date("2000-01-01"))
> aggregate(z, as.numeric(format(time(z), "%Y")), mean)
  2000   2001   2002
1182.5 1548.0 1865.5

See:
  vignette("zoo")
  vignette("zoo-quickref")
  ?zooreg
  ?aggregate.zoo
for more info.

On Nov 17, 2007 7:51 AM, lucia <lucia at thedietdiary.com> wrote:
> Hello,
> I'm very new to R, and so my question is simple.
>
> I have data record with 80 years of daily temperatures in one long
> string.  The dates are also recorded, in YYMMDD format.  I'd like to
> learn an elegant simple way to pull out the annual averages.
> (Obviously, every 4th year has 366 days.)
>
> I know I can set up a formal loop to create annual records and then
> average. But R seems to have such neat methods, is there some better
> way to do this?
>
> Lucia
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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