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

Brian G. Peterson brian at braverock.com
Tue Feb 1 21:02:30 CET 2011


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



More information about the R-SIG-Finance mailing list