[R] How to turn Time Series daily values into weekly means (aggregate?)

jim holtman jholtman at gmail.com
Thu Jun 26 04:24:30 CEST 2008


Try this:

y.ts <- ts(y, frequency=7)  # make data weekly
aggregate(y.ts, FUN=mean)

to get your 15 minute readings into a day, this should work:

y.ts <- ts(y, frequency=96)
aggregate(y.ts, FUN=mean)

This is fine as long as everything is evenly spaced.  It might be
better if you had the time of the observation so that it would take
care of missing data when doing the aggregation.

On Wed, Jun 25, 2008 at 9:57 PM, stephen sefick <ssefick at gmail.com> wrote:
> #this is a daily series of precipitation data.  I would like to condense it
> into weekly means.  How can I do this
> #as a side note I would like to do this same thing to two years worth of
> fifteen minute interval data and make it into
> #a series of daily averages (there are 96 readings per day)
> #is aggregate the right function? or...
>
> y <- c(1.23, 0, 0, 0, 0, 0, 0, 0, 0, 0.27, 0, 0.29, 0, 0, 0, 0.43,
> 0.2, 0, 0, 0.06, 0.05, 0.36, 0.06, 0, 0, 0, 0, 0.16, 0, 0, 0,
> 0.17, 0, 0.31, 0, 0, 0.01, 0, 0, 0, 0.19, 0, 0, 0, 0, 0, 0, 0.01,
> 0, 0.13, 0, 0.58, 0.07, 0, 1.48, 0.14, 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0.03, 0, 0, 0, 0.39, 0, 0, 0, 0, 0, 0.12, 2.79, 0, 0,
> 0, 0, 0, 0, 0.01, 0, 0, 0, 0.19, 0, 0, 0, 0, 0, 0, 0.89, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0.64, 0, 0, 0.31, 0, 0, 0, 0.31, 0, 0,
> 0, 0, 0, 0, 0, 0.08, 0.37, 0, 0.49, 0, 0, 0, 0, 0, 0, 0.49, 0,
> 0, 0, 0.15, 0, 0, 0, 0, 0, 0, 0.07, 0, 0, 0, 0, 0, 0, 0, 1.43,
> 0.02, 0, 0, 0, 0, 0, 0, 0, 0, 0.81, 1.9, 1.02, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0.35, 0.89, 0, 0.01, 0, 0, 0, 0, 0, 0, 0, 0.22,
> 0, 0, 0, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 0, 0, 0, 0.21, 0.12, 0.58,
> 0, 0, 0, 0, 0.28, 0.09, 0, 0, 0, 0, 0.3, 0, 0, 0, 0, 0, 0.5,
> 0, 0.59, 0, 0, 0, 0.01, 0, 0, 0.04, 1.4, 0, 0.6, 0, 2.06, 0,
> 0, 0, 0, 0, 0.39, 0, 0, 0, 1.06, 0, 0.26, 0, 0.01, 0, 0, 0, 0,
> 0, 1.17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.18, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0.02, 0, 0.05, 0, 0, 0.84, 0, 0, 0, 0, 0.04, 0.06,
> 0, 0, 0, 0, 0.46, 0, 0, 0, 0, 0.91, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0.32, 0, 0, 0, 0, 0, 0, 0, 1.29, 0.3, 0, 0, 0, 0.01, 0.61,
> 0.34, 0, 0, 0, 0, 0, 0, 0, 0.08, 0.1, 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0.02, 0, 0, 0, 0, 0, 0, 0, 0.28, 2.65, 0, 0, 2.21, 0,
> 0, 0, 0, 0.06, 0.12, 0.16, 0, 0, 0, 0.52, 0, 0.34, 0.18, 0, 0,
> 0, 0, 0, 0, 0, 0.03, 0.01, 0.28, 0, 0, 0.44, 0.75, 0, 0, 0, 0,
> 0, 0.33, 0, 0, 0, 1.28, 0.03, 0, 0, 0, 0, 0, 0, 0.01, 0, 0, 0,
> 0.69, 0, 0, 0, 0, 0, 0, 0, 0.57, 0, 0, 0, 0.24, 0, 0, 0)
>
> y.ts <- ts(y, frequency=30)
> aggregate(y.ts, nf=7, FUN=mean)
>
> --
> Let's not spend our time and resources thinking about things that are so
> little or so large that all they really do for us is puff us up and make us
> feel like gods. We are mammals, and have not exhausted the annoying little
> problems of being mammals.
>
> -K. Mullis
>
>        [[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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list