Santosh Srinivas
2010-Nov-05 15:21 UTC
[R] Extracting data only for particular index values from a zoo structure
Hello All, I have a zoo structure as follows:> dput(tMRet)structure(c(0.00138742474397713, -0.0309023681475112, 0.0390276302410908, 0.0832282874685357, -0.00315002033871414, -0.0158548785709138, -0.0410876001496389, -0.0503189291168807, 0.00229628598547049, 0.112348434473647, 0.0760004696254608, 0.100820586425124, 0.0803767768546975, 0.0967805566974766, 0.054288018745434, 0.106415042990242, 0.0848339767191362, -0.0293833917022324, -0.0355384394730908, 0.0398272106900921), .Dim = c(20L, 1L), .Dimnames = list(c("Sep 2002", "Oct 2002", "Nov 2002", "Dec 2002", "Jan 2003", "Feb 2003", "Mar 2003", "Apr 2003", "May 2003", "Jun 2003", "Jul 2003", "Aug 2003", "Sep 2003", "Oct 2003", "Nov 2003", "Dec 2003", "Jan 2004", "Feb 2004", "Mar 2004", "Apr 2004"), "Close"), index structure(c(2002.66666666667, 2002.75, 2002.83333333333, 2002.91666666667, 2003, 2003.08333333333, 2003.16666666667, 2003.25, 2003.33333333333, 2003.41666666667, 2003.5, 2003.58333333333, 2003.66666666667, 2003.75, 2003.83333333333, 2003.91666666667, 2004, 2004.08333333333, 2004.16666666667, 2004.25 ), class = "yearmon"), class = "zoo") I want to extract only the values for say "November" but may span multiple years. How can I use conditions on the zoo index? For info, I did an aggregate over daily data to reach here. Have a great weekend! Thanks. S
Gabor Grothendieck
2010-Nov-05 15:29 UTC
[R] Extracting data only for particular index values from a zoo structure
On Fri, Nov 5, 2010 at 11:21 AM, Santosh Srinivas <santosh.srinivas at gmail.com> wrote:> Hello All, > > I have a zoo structure as follows: >> dput(tMRet) > structure(c(0.00138742474397713, -0.0309023681475112, 0.0390276302410908, > 0.0832282874685357, -0.00315002033871414, -0.0158548785709138, > -0.0410876001496389, -0.0503189291168807, 0.00229628598547049, > 0.112348434473647, 0.0760004696254608, 0.100820586425124, > 0.0803767768546975, > 0.0967805566974766, 0.054288018745434, 0.106415042990242, > 0.0848339767191362, > -0.0293833917022324, -0.0355384394730908, 0.0398272106900921), .Dim = c(20L, > > 1L), .Dimnames = list(c("Sep 2002", "Oct 2002", "Nov 2002", "Dec 2002", > "Jan 2003", "Feb 2003", "Mar 2003", "Apr 2003", "May 2003", "Jun 2003", > "Jul 2003", "Aug 2003", "Sep 2003", "Oct 2003", "Nov 2003", "Dec 2003", > "Jan 2004", "Feb 2004", "Mar 2004", "Apr 2004"), "Close"), index > structure(c(2002.66666666667, > 2002.75, 2002.83333333333, 2002.91666666667, 2003, 2003.08333333333, > 2003.16666666667, 2003.25, 2003.33333333333, 2003.41666666667, > 2003.5, 2003.58333333333, 2003.66666666667, 2003.75, 2003.83333333333, > 2003.91666666667, 2004, 2004.08333333333, 2004.16666666667, 2004.25 > ), class = "yearmon"), class = "zoo") > > I want to extract only the values for say "November" but may span multiple > years. > How can I use conditions on the zoo index? >Try this:> tMRet[cycle(tMRet) == 11,, drop = FALSE]Close Nov 2002 0.03902763 Nov 2003 0.05428802 If you just want a 1 dimensional object omit the drop=FALSE part. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Santosh Srinivas
2010-Nov-05 15:54 UTC
[R] Extracting data only for particular index values from a zoo structure
Thanks Gabor for pointing in the right direction. Looked up cycle and the doc is tough to understand " cycle gives the positions in the cycle of each observation." ... how is cycle defined. I just extended your idea to make it readable for an avg. user in the following way mRet[format(index(mRet),"%m")==11] -----Original Message----- From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] Sent: 05 November 2010 20:59 To: Santosh Srinivas Cc: r-help at r-project.org Subject: Re: [R] Extracting data only for particular index values from a zoo structure On Fri, Nov 5, 2010 at 11:21 AM, Santosh Srinivas <santosh.srinivas at gmail.com> wrote:> Hello All, > > I have a zoo structure as follows: >> dput(tMRet) > structure(c(0.00138742474397713, -0.0309023681475112, 0.0390276302410908, > 0.0832282874685357, -0.00315002033871414, -0.0158548785709138, > -0.0410876001496389, -0.0503189291168807, 0.00229628598547049, > 0.112348434473647, 0.0760004696254608, 0.100820586425124, > 0.0803767768546975, > 0.0967805566974766, 0.054288018745434, 0.106415042990242, > 0.0848339767191362, > -0.0293833917022324, -0.0355384394730908, 0.0398272106900921), .Dim c(20L, > > 1L), .Dimnames = list(c("Sep 2002", "Oct 2002", "Nov 2002", "Dec 2002", > "Jan 2003", "Feb 2003", "Mar 2003", "Apr 2003", "May 2003", "Jun 2003", > "Jul 2003", "Aug 2003", "Sep 2003", "Oct 2003", "Nov 2003", "Dec 2003", > "Jan 2004", "Feb 2004", "Mar 2004", "Apr 2004"), "Close"), index > structure(c(2002.66666666667, > 2002.75, 2002.83333333333, 2002.91666666667, 2003, 2003.08333333333, > 2003.16666666667, 2003.25, 2003.33333333333, 2003.41666666667, > 2003.5, 2003.58333333333, 2003.66666666667, 2003.75, 2003.83333333333, > 2003.91666666667, 2004, 2004.08333333333, 2004.16666666667, 2004.25 > ), class = "yearmon"), class = "zoo") > > I want to extract only the values for say "November" but may span multiple > years. > How can I use conditions on the zoo index? >Try this:> tMRet[cycle(tMRet) == 11,, drop = FALSE]Close Nov 2002 0.03902763 Nov 2003 0.05428802 If you just want a 1 dimensional object omit the drop=FALSE part. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com