Of course you have to first convert timeSeries to xts with as.xts ... but
you probably knew that already

Best,
Jeff

On Tue, Apr 6, 2010 at 3:21 PM, Jeff Ryan <jeff.a.ryan@gmail.com> wrote:

> I will provide some examples, which may or may not help, since I am not
> 100% sure of your question:
>
>
> > library(xts)
> > library(quantmod)
> Loading required package: Defaults
> Loading required package: TTR
> > getSymbols("AAPL",from="2009-01-01")
> [1] "AAPL"
>
> > str(AAPL)
> An ‘xts’ object from 2009-01-02 to 2010-04-05 containing:
>   Data: num [1:315, 1:6] 85.9 93.2 96 91.8 90.4 ...
>  - attr(*, "dimnames")=List of 2
>   ..$ : NULL
>   ..$ : chr [1:6] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
>   Indexed by objects of class: [Date] TZ: America/Chicago
>   xts Attributes:
> List of 2
>  $ src    : chr "yahoo"
>  $ updated: POSIXct[1:1], format: "2010-04-06 15:12:54"
>
> > head(AAPL) # just the data ..
>            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
> AAPL.Adjusted
> 2009-01-02     85.88     91.04    85.16      90.75    26643400
> 90.75
> 2009-01-05     93.17     96.18    92.71      94.58    42200300
> 94.58
> 2009-01-06     95.95     97.17    92.39      93.02    46046800
> 93.02
> 2009-01-07     91.81     92.50    90.26      91.01    26894600
> 91.01
> 2009-01-08     90.43     93.15    90.04      92.70    23912000
> 92.70
> 2009-01-09     93.21     93.38    90.14      90.58    19530200
> 90.58
>
> # convert to monthly
> > head(to.monthly(AAPL))
>          AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
> Jan 2009     85.88     97.17    78.20      90.13   646492000         90.13
> Feb 2009     89.10    103.00    86.51      89.31   495266100         89.31
> Mar 2009     88.12    109.98    82.33     105.12   550857000        105.12
> Apr 2009    104.09    127.20   103.89     125.83   422202900        125.83
> May 2009    125.80    135.90   119.38     135.81   337755100        135.81
> Jun 2009    136.47    146.40   132.88     142.43   452699000        142.43
>
> # to quarterly
> > head(to.quarterly(AAPL))
>         AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
> 2009 Q1     85.88    109.98    78.20     105.12  1692615100        105.12
> 2009 Q2    104.09    146.40   103.89     142.43  1212657000        142.43
> 2009 Q3    143.50    188.90   134.42     185.35  1039163600        185.35
> 2009 Q4    185.35    213.95   180.70     210.73  1171604300        210.73
> 2010 Q1    213.43    237.48   190.25     235.00  1360348100        235.00
> 2010 Q2    237.41    238.73   232.75     238.49    45869100        238.49
>
> # to monthly, this time every 6 months
> > head(to.period(AAPL,'months',k=6))
>            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
> AAPL.Adjusted
> 2009-06-30     85.88    146.40    78.20     142.43  2905272100
>  142.43
> 2009-12-31    143.50    213.95   134.42     210.73  2210767900
>  210.73
> 2010-04-05    213.43    238.73   190.25     238.49  1406217200
>  238.49
>
> # weekly..
> > head(to.weekly(AAPL))
>            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
> AAPL.Adjusted
> 2009-01-02     85.88     91.04    85.16      90.75    26643400
> 90.75
> 2009-01-09     93.17     97.17    90.04      90.58   158583900
> 90.58
> 2009-01-16     90.46     90.99    80.05      82.33   189894200
> 82.33
> 2009-01-23     81.93     90.00    78.20      88.36   149374400
> 88.36
> 2009-01-30     88.86     95.00    88.30      90.13   121996100
> 90.13
> 2009-02-06     89.10    100.00    88.90      99.72   121515500
> 99.72
>
> # just 2009-12  (see ?ISO8601 and the xts vignette for more)
> > AAPL['200912']
>            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
> AAPL.Adjusted
> 2009-12-01    202.24    202.77   196.83     196.97    16634400
>  196.97
> 2009-12-02    198.96    201.42   195.75     196.23    25545000
>  196.23
> 2009-12-03    197.42    198.98   196.27     196.48    16025700
>  196.48
> 2009-12-04    199.70    199.88   190.28     193.32    29531600
>  193.32
> 2009-12-07    193.32    193.77   188.68     188.95    25527100
>  188.95
> 2009-12-08    189.36    192.35   188.70     189.87    24657100
>  189.87
> 2009-12-09    191.28    198.16   190.31     197.80    24456500
>  197.80
> 2009-12-10    199.50    199.70   196.12     196.43    17488200
>  196.43
> 2009-12-11    197.78    198.00   193.43     194.67    15349100
>  194.67
> 2009-12-14    195.37    197.43   192.56     196.98    17706800
>  196.98
> 2009-12-15    195.83    197.51   193.27     194.17    14980700
>  194.17
> 2009-12-16    195.10    196.50   194.55     195.03    12606600
>  195.03
> 2009-12-17    194.26    195.00   191.00     191.86    13887100
>  191.86
> 2009-12-18    193.17    195.50   192.60     195.43    21741800
>  195.43
> 2009-12-21    196.05    199.75   195.67     198.23    21853800
>  198.23
> 2009-12-22    199.44    200.85   198.66     200.36    12482700
>  200.36
> 2009-12-23    201.20    202.38   200.81     202.10    12340200
>  202.10
> 2009-12-24    203.55    209.35   203.35     209.04    17888900
>  209.04
> 2009-12-28    211.72    213.95   209.61     211.61    23020200
>  211.61
> 2009-12-29    212.63    212.72   208.73     209.10    15900200
>  209.10
> 2009-12-30    208.83    212.00   208.31     211.64    14717300
>  211.64
> 2009-12-31    213.13    213.35   210.56     210.73    12586100
>  210.73
>
>
> # just subsetting at the end of each period, instead of fancy aggregation
> > to.period(AAPL,'months',endpoints=TRUE)
>            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
> AAPL.Adjusted
> 2009-01-30     85.88     97.17    78.20      90.13   646492000
> 90.13
> 2009-02-27     89.10    103.00    86.51      89.31   495266100
> 89.31
> 2009-03-31     88.12    109.98    82.33     105.12   550857000
>  105.12
> 2009-04-30    104.09    127.20   103.89     125.83   422202900
>  125.83
> 2009-05-29    125.80    135.90   119.38     135.81   337755100
>  135.81
> 2009-06-30    136.47    146.40   132.88     142.43   452699000
>  142.43
> 2009-07-31    143.50    165.00   134.42     163.39   390790600
>  163.39
> 2009-08-31    165.21    172.49   159.42     168.21   291611600
>  168.21
> 2009-09-30    167.99    188.90   164.11     185.35   356761400
>  185.35
> 2009-10-30    185.35    208.71   180.70     188.50   462720600
>  188.50
> 2009-11-30    189.80    208.00   185.57     199.91   301956600
>  199.91
> 2009-12-31    202.24    213.95   188.68     210.73   406927100
>  210.73
> 2010-01-29    213.43    215.59   190.25     192.06   541749800
>  192.06
> 2010-02-26    192.37    205.17   190.85     204.62   384860000
>  204.62
> 2010-03-31    205.75    237.48   205.45     235.00   433738300
>  235.00
> 2010-04-05    237.41    238.73   232.75     238.49    45869100
>  238.49
>
>
> # and finally ranges...
> > AAPL['200912/20100110']
>            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
> AAPL.Adjusted
> 2009-12-01    202.24    202.77   196.83     196.97    16634400
>  196.97
> 2009-12-02    198.96    201.42   195.75     196.23    25545000
>  196.23
> 2009-12-03    197.42    198.98   196.27     196.48    16025700
>  196.48
> 2009-12-04    199.70    199.88   190.28     193.32    29531600
>  193.32
> 2009-12-07    193.32    193.77   188.68     188.95    25527100
>  188.95
> 2009-12-08    189.36    192.35   188.70     189.87    24657100
>  189.87
> 2009-12-09    191.28    198.16   190.31     197.80    24456500
>  197.80
> 2009-12-10    199.50    199.70   196.12     196.43    17488200
>  196.43
> 2009-12-11    197.78    198.00   193.43     194.67    15349100
>  194.67
> 2009-12-14    195.37    197.43   192.56     196.98    17706800
>  196.98
> 2009-12-15    195.83    197.51   193.27     194.17    14980700
>  194.17
> 2009-12-16    195.10    196.50   194.55     195.03    12606600
>  195.03
> 2009-12-17    194.26    195.00   191.00     191.86    13887100
>  191.86
> 2009-12-18    193.17    195.50   192.60     195.43    21741800
>  195.43
> 2009-12-21    196.05    199.75   195.67     198.23    21853800
>  198.23
> 2009-12-22    199.44    200.85   198.66     200.36    12482700
>  200.36
> 2009-12-23    201.20    202.38   200.81     202.10    12340200
>  202.10
> 2009-12-24    203.55    209.35   203.35     209.04    17888900
>  209.04
> 2009-12-28    211.72    213.95   209.61     211.61    23020200
>  211.61
> 2009-12-29    212.63    212.72   208.73     209.10    15900200
>  209.10
> 2009-12-30    208.83    212.00   208.31     211.64    14717300
>  211.64
> 2009-12-31    213.13    213.35   210.56     210.73    12586100
>  210.73
> 2010-01-04    213.43    214.50   212.38     214.01    17633200
>  214.01
> 2010-01-05    214.60    215.59   213.25     214.38    21496600
>  214.38
> 2010-01-06    214.38    215.23   210.75     210.97    19720000
>  210.97
> 2010-01-07    211.75    212.00   209.05     210.58    17040400
>  210.58
> 2010-01-08    210.30    212.00   209.06     211.98    15986100
>  211.98
>
>
> HTH
> Jeff
>
> ***** R/Finance is happening in 2 weeks!!  http://www.RinFinance.com *****
>
> On Tue, Apr 6, 2010 at 2:25 PM, Jeff Hamann <jeff.hamann@moneytree.com>wrote:
>
>> I'm a newbie with timeSeries and I'm trying to subset data from a
>> timeSeries objects (by month, quarter, etc.)
>>
>> GMT
>>                  ^GSPC          RYJ          NFO           STH
>>  CVY
>> 2007-01-03  0.013961173 -0.009841523  0.024217982 -0.0003857281
>>  0.018018506
>> 2007-02-01 -0.022088306 -0.006062295 -0.003042984 -0.0120319713
>> -0.006430890
>> 2007-03-01  0.009930484 -0.005543251  0.021854675  0.0143441110
>>  0.003220615
>> 2007-04-02  0.042379836  0.055675009  0.041254253  0.0042250879
>>  0.037859411
>> 2007-05-01  0.032030724  0.043216601  0.044762718  0.0372433489
>>  0.028772691
>> 2007-06-01 -0.017976931  0.000000000 -0.021431861 -0.0077850254
>> -0.018651593
>>                   XRO          ONEQ          FBT          FDV          FDN
>> 2007-01-03  0.04083675  0.0179801641  0.024199865  0.024397067
>>  0.048071606
>> 2007-02-01 -0.01103803 -0.0194842469 -0.040227932 -0.007760824
>> -0.012337957
>>
>> and I'm kind of a newbie with timeSeries.
>>
>> Before I spend a few days hacking out suboptimal solutions, I thought I
>> might ask the group where I might find a decent tutorial on sub-setting
>> timeSeries using only date ranges.
>>
>> I have monthly, weekly, and daily datasets and need to grad monthly,
>> weekly, and daily subsets for by month, quarter, etc.
>>
>> I'm sure this will elicit an RTFM response, but please, pretty please,
>> with sugar on top, be gentle. I'm new here.
>>
>> Thanks,
>> Jeff.
>>
>>
>> Jeff D. Hamann, PhD
>> 2430 NW Professional Drive
>> Corvallis, Oregon 97330
>> 877-421-9815
>> jeff.hamann[at]moneytree[dot]com
>> http://www.moneytree.com
>>
>> Full featured flexible financial plans in minutes.
>> Simple or advanced. Goal or cash flow. Desktop or online.
>> Free downloads, videos, & online trials.
>>
>>
>>
>>        [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-SIG-Finance@stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R questions
>> should go.
>>
>
>
>
> --
> Jeffrey Ryan
> jeffrey.ryan@insightalgo.com
>
> ia: insight algorithmics
> www.insightalgo.com
>
>


-- 
Jeffrey Ryan
jeffrey.ryan@insightalgo.com

ia: insight algorithmics
www.insightalgo.com

	[[alternative HTML version deleted]]

