[R-SIG-Finance] Selecting a subset of date range

Jeffrey Ryan jeffrey.ryan at lemnica.com
Tue Feb 1 21:07:49 CET 2011


You can also use the subsetting in xts without using xts per se
(though a proper time-series is really the best idea)

> library(xts)
Loading required package: zoo
> x <- timeBasedSeq("20000101/20100101")
> x <- xts(rep(1, length(x)), x)

> str( x['20070101/200712'] )
An ‘xts’ object from 2007-01-01 to 2007-12-31 containing:
  Data: num [1:365, 1] 1 1 1 1 1 1 1 1 1 1 ...
  Indexed by objects of class: [Date] TZ: America/Chicago
  xts Attributes:
 NULL

> str( x['20070101/20071201'] )
An ‘xts’ object from 2007-01-01 to 2007-12-01 containing:
  Data: num [1:335, 1] 1 1 1 1 1 1 1 1 1 1 ...
  Indexed by objects of class: [Date] TZ: America/Chicago
  xts Attributes:
 NULL

> str( index(x['20070101/20071201']) )
Class 'Date'  num [1:335] 13514 13515 13516 13517 13518 ...


Lots more in the xts vignette and in the archives here.

In addition to the ISO8601 semantics which make it easier to
read/understand, you also benefit from a very fast binary search and C
memcpy - effectively the fastest way to subset possible.  The column
extraction alone is 10x faster than an R matrix.  And the binary
search is O(log n) - so even billions of observations are able to be
subset with precision, and nearly instantly (well, linear in time with
the length of the resultant subset)

Best,
Jeff

On Tue, Feb 1, 2011 at 2:02 PM, Brian G. Peterson <brian at braverock.com> wrote:
> On 02/01/2011 01:56 PM, Megh Dal wrote:
>>
>> If I have a sequence of dates then how can I select a subset of this
>> sequence? For example suppose, I have following sequence of dates:
>>
>> dat_seq<- seq(as.Date("2000-01-01"), as.Date("2010-01-01"), by="1 day")
>>
>> Now I want to select all dates of above sequence (this sequence may not be
>> continuous like above!) from "as.Date("2007-01-01")" to
>> "as.Date("2007-12-01"))", how can I achieve that? I have tried following
>> without any success:
>
> As has been said many times on this list and elsewhere, use xts for
> financial time series.  Subsetting is intuitive, fast, and reliable:
>
> # starting from your sequence
> dat_seq <- seq(as.Date("2000-01-01"), as.Date("2010-01-01"), by="1 day")
> #create some dummy data, it need not be continuous
> x<-xts(rnorm(length(dat_seq)),order.by=dat_seq)
> #subset using ISO date subsetting
> x['2007-01-01::2007-12-01']
>
> Cheers,
>
>  - Brian
>
> --
> Brian G. Peterson
> http://braverock.com/brian/
> Ph: 773-459-4973
> IM: bgpbraverock
>
> _______________________________________________
> R-SIG-Finance at r-project.org 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 lemnica.com

www.lemnica.com



More information about the R-SIG-Finance mailing list