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. Any advice on this matter would be greatly appreciated. Many thanks and kind regards, Wesley Wesley Roberts PhD. Researcher: Earth Observation Natural Resources & the Environment (NRE) CSIR Tel: +27 (0)21 888-2490 Fax: +27 (0)21 888-2693 skype: roberts-w -- This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard. The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html. This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
Gabor Grothendieck
2011-Sep-22 11:52 UTC
[R] Subsetting a zooreg object using window / subset
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
On Thu, 22 Sep 2011, Gabor Grothendieck wrote:> 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"))An alternative route might be to go via the "mon" component of POSIXlt, e.g., as.POSIXlt(time(aa))$mon %in% c(11, 0, 1) instead of seas == 1 etc.> 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 > > ______________________________________________ > 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. >