[R-SIG-Finance] Selecting once a month from a xts series

Jeff Ryan jeff.a.ryan at gmail.com
Thu Mar 25 02:24:39 CET 2010


If you want something like a random date, with a max of one per month,
you can try:

library(quantmod) #for getSymbols
getSymbols("AAPL")

do.call(rbind,
          lapply(sample(split(AAPL,"months"), 10),
                   function(x) x[sample(1:NROW(x),1)]
                   )
          )

           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2007-07-11    132.07    133.70   131.31     132.39    29349000        132.39
2007-09-14    136.57    138.98   136.20     138.81    21690000        138.81
2007-11-19    166.10    168.20   162.10     163.95    41196800        163.95
2008-01-22    148.06    159.98   146.00     155.64    86955500        155.64
2008-04-10    151.13    155.42   150.60     154.55    34134400        154.55
2008-06-20    179.35    181.00   175.00     175.27    31727400        175.27
2008-07-16    170.20    172.93   168.60     172.81    26706800        172.81
2009-05-11    127.37    130.96   127.12     129.57    14452100        129.57
2009-10-09    188.97    190.70   188.62     190.47    10474000        190.47
2009-11-17    206.08    207.44   205.00     207.00    14161200        207.00
>

split.xts is the key, the rest is just regular R code; which means
there's more than one way to do it.

If you want to assure one per month, just make sure you set the "10"
to the nmonths of the data.

> do.call(rbind,lapply(sample(split(AAPL,"months"), nmonths(AAPL)), function(x) x[sample(1:NROW(x),1)]))
           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2007-01-04     84.05     85.95    83.82      85.66    30259300         85.66
2007-02-02     84.12     85.25    83.70      84.75    22197500         84.75
2007-03-13     89.41     90.60    88.40      88.40    30996100         88.40
2007-04-04     94.94     95.14    94.13      94.27    17028000         94.27
2007-05-04    100.80    101.60   100.50     100.81    13642400        100.81
2007-06-28    122.36    122.49   120.00     120.56    29933700        120.56
2007-07-24    138.88    141.00   134.15     134.89    64117600        134.89
2007-08-10    123.12    127.75   120.30     125.00    50383900        125.00
2007-09-12    135.99    139.40   135.75     136.85    36527500        136.85
2007-10-16    165.54    170.18   165.15     169.58    38136800        169.58
2007-11-28    176.82    180.60   175.35     180.22    41104000        180.22
2007-12-13    190.19    192.12   187.82     191.83    30879200        191.83
2008-01-22    148.06    159.98   146.00     155.64    86955500        155.64
2008-02-19    125.99    126.75   121.44     122.18    35894500        122.18
2008-03-19    133.12    134.29   129.67     129.67    36090600        129.67
2008-04-15    149.40    149.72   145.72     148.38    24929900        148.38
2008-05-08    183.77    186.50   183.07     185.06    32110200        185.06
2008-06-26    174.07    174.84   168.01     168.26    31057500        168.26
2008-07-16    170.20    172.93   168.60     172.81    26706800        172.81
2008-08-04    156.60    157.90   152.91     153.23    21161700        153.23
2008-09-03    166.84    168.68   164.00     166.96    26244100        166.96
2008-10-06     91.96     98.78    87.54      98.14    75264900         98.14
2008-11-24     85.21     94.79    84.84      92.95    51509200         92.95
2008-12-10     97.87     99.49    96.50      98.21    33501700         98.21
2009-01-23     86.82     89.87    86.50      88.36    27277500         88.36
2009-02-25     89.86     92.92    89.25      91.16    29751900         91.16
2009-03-25    107.58    108.36   103.86     106.49    23093500        106.49
2009-04-06    114.94    118.75   113.28     118.45    23502300        118.45
2009-05-06    133.33    133.50   130.22     132.50    16912100        132.50
2009-06-05    145.31    146.40   143.21     144.67    22597000        144.67
2009-07-30    161.70    164.72   161.50     162.79    16771600        162.79
2009-08-10    165.66    166.60   163.66     164.72    10724800        164.72
2009-09-02    164.62    167.61   164.11     165.18    13008900        165.18
2009-10-22    204.70    207.85   202.51     205.20    28264000        205.20
2009-11-30    201.11    201.68   198.77     199.91    15173500        199.91
2009-12-23    201.20    202.38   200.81     202.10    12315900        202.10
2010-01-26    205.95    213.71   202.58     205.94    66605200        205.94
2010-02-11    194.88    199.75   194.06     198.67    19643400        198.67
2010-03-02    209.93    210.83   207.74     208.85    20220200        208.85



HTH
Jeff

On Wed, Mar 24, 2010 at 8:18 PM, Brian G. Peterson <brian at braverock.com> wrote:
> Worik Stanton wrote:
>>
>> With a xts series of daily prices I want to select samples from once a
>> month.
>>
>> Given that p.xts is the xts series
>>
>>> p.xts[1,]
>>
>>           Adj.Close
>> 2003-01-01       4.3
>>
>>> p.xts[length(p.xts[,1]),]
>>
>>           Adj.Close
>> 2010-03-05      2.25
>>
>>
>>
>> What I would like to do is...
>>
>>> i <- seq(from=index(p.xts[1,]), to=index(p.xts[length(p.xts[,1]),]),
>>> by="month")
>>> m.xts <- p.xts[i,]
>>
>> Error in `[.xts`(p.xts, i, ) : i is out of range
>>
>> Of course some of the dates in 'i' are not in index(p  .xts)
>>
>> Is there a simple solution?
>
> ?endpoints
>
>
> --
> Brian G. Peterson
> http://braverock.com/brian/
> Ph: 773-459-4973
> IM: bgpbraverock
>
> _______________________________________________
> R-SIG-Finance at 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 at insightalgo.com

ia: insight algorithmics
www.insightalgo.com



More information about the R-SIG-Finance mailing list