[R] Seasonal and 11-day subset for zoo object

Gabor Grothendieck ggrothendieck at gmail.com
Thu Sep 8 05:35:33 CEST 2011


On Wed, Sep 7, 2011 at 9:59 PM, Katrina Bennett <kebennett at alaska.edu> wrote:
> I have a zooreg object and I want to be able to generate a value for seasons
> and 11-day composites paste it onto my zoo data frame, along with year,
> month and days.
>
> Right now I have the following to work from:
>
> eg. dat.zoo.mdy <- with(month.day.year(time(dat.zoo)), cbind(dat.zoo, year,
> month, day, quarter = (month - 1) %/% 3 + 1, dow =
> as.numeric(format(time(dat.zoo), "%w"))))
>
> For the seasons, I have been trying to replace 'quarter' with a seasonal
> value of "1" for Dec-Jan-Feb, "2" for Mar-Apr-May, "3" for Jun-Jul-Aug, "4"
> for Sep-Oct-Nov.
>
> dat.zoo.mdy <- with(month.day.year(time(dat.zoo)), cbind(dat.zoo, year,
> month, day,
> season=for(i in nrow(dat.zoo.mdy)) {
>         if (month[i] == 12) {
>         quarter[i]=1
>         } else if (month[i] == 3) {
>         quarter[i]=2
>         } else if (month[i] == 6) {
>         quarter[i]=3
>         } else quarter[i]=4 }, dow = as.numeric(format(time(dat.zoo),
> "%w"))))
>
> However, this gives me the error: "Error in zoo(structure(x, dim = dim(x)),
> index(x), ...) :
>  “x” : attempt to define illegal zoo object"
>
> I'd like to get an 11-day value as well to replace the dow in the first
> example, but I'm still trying to figure out if there is an easy way to do
> this in zoo.
>

dat.zoo and "11 days composite" in the question were not defined but
we can get the seasons by calculating the quarter of the following
month:

> d <- seq(as.Date("2011-01-01"), length = 12, by = "month")
> as.numeric(format(as.yearqtr(as.yearmon(d) + 1/12), "%q"))
 [1] 1 1 2 2 2 3 3 3 4 4 4 1


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list