[R] Subsetting a zooreg object using window / subset
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Sep 22 13:52:49 CEST 2011
On Thu, Sep 22, 2011 at 6:48 AM, Wesley Roberts <wroberts at csir.co.za> wrote:
> Dear R users,
>
>
> I am currently working in subsetting a zooreg() object using either window or subset. I have a solution but it may be a bit cumbersome when I start working with actual data. Your inputs would be greatly appreciated.
>
> Example: I have a zooreg() object that starts in 1997 and ends in 2001. This object contains daily data for the 4 years
>
> aa<-zooreg(1:1825,start=as.Date("1997-01-01"))
>
> My aim is to subset the data according to seasons (Southern Hemisphere) for continuous years: December - January - February (DJF-Summer: 1997-2001), March - April - May (MAM-Autumn: 1997-2001), June - July - August (JJA-Winter: 1997-2001), September - October - November (SON-Spring: 1997-2001) thereby analysing the seasons data only for all years. The example below is only for DJF but I would like to replicate the analysis for each season.
>
> My solution so far uses subset to select the monthly data for each year and then rbind() the results.
>
> bb <- subset(aa, index(aa)>=as.Date("1998-12-01")&index(aa)<=as.Date("1999-02-28"))
> cc <- subset(aa, index(aa)>=as.Date("1999-12-01")&index(aa)<=as.Date("2000-02-28"))
> dd <- subset(aa, index(aa)>=as.Date("2000-12-01")&index(aa)<=as.Date("2001-02-28"))
>
> ee<- rbind(bb,cc,dd)
>
> The method above appears to do the job just fine except that I have around 30 locations (catchments) each with varying data availability and some with over 20 years worth of data. Ideally I would like to combine the second set of commands into a single command where I specify the start and end year and the months that I am interested in.
>
This gives the season (1 = djf, 2 = mam, 3 = jja, 5 = son):
seas <- as.numeric(format(as.yearqtr(as.yearmon(time(aa)) + 1/12), "%q"))
and this picks out djf:
aa[seas == 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