Skip to content
Prev 2002 / 15274 Next

business day to monthly or quarterly aggregation.

A quick two functions to get the quarters (and more), though this
doesn't address your full issue (or the issue Patrick brings up):

This is from the new xts package we have put up on CRAN and on R-forge
( http://r-forge.r-project.org/projects/xts/ ).  the as.xts is what
you'll need xts for, though I am sure you can make a non-generic
solution instead.

`endpoints` <-
function(x,on='months') {
  x <- as.xts(x)
  if(on=='quarters') {
    xi <- (as.POSIXlt(index(x))$mon%/%3) + 1
    c(0,which(diff(xi) != 0),NROW(x))
  } else {
    on.opts <- list(secs='%S',seconds='%S',mins='%M',minutes='%M',
                    hours='%H',days='%j',
                    weeks='%W',months='%m',years='%y')
    c(0,which(diff(as.numeric(format(index(x),on.opts[[on]]))) != 0),NROW(x))
  }
}

`startof` <-
function(x,by='months') {
  ep <- endpoints(x,on=by)
  (ep+1)[-length(ep)]
}

A quick intro to xts:

An extension of zoo to enforce time-based indexing, while allowing for
arbitrary attributes to be added on to an object.  The idea is to
allow for quick time based subsetting, as well as clean conversion
among the different R data classes, both back and forth, while
maintaining all the available object information.  It basically makes
time-series life easy in R by standardizing time-series behavior to
'zoo-like' behavior.  And all the greatness of zoo is preserved!

The SVN code has some very handy sub-setting methods in the works:

library(quantmod); library(its)
getSymbols("QQQQ",src='yahoo',return.class='its')

as.xts(QQQQ)['2007']  # all of 07
as.xts(QQQQ)['2007-01']  # just January
as.xts(QQQQ)['2007-03::2007-03-20']  # March 07 - beginning of month
to the 20th (even if the 20th doesn't exist)

reclass(as.xts(QQQQ))  # though contrived returns the [now xts] QQQQ
object back to its original class (whatever that may have been - ts,
its, timeSeries, matrix, data.frame, zoo  - exactly as found).


The functions are not yet [exported] in the xts CRAN release - though
in the SVN on r-forge.

Jeff
On Jan 10, 2008 9:02 AM, Murali Menon <feanor0 at hotmail.com> wrote: