Skip to content
Prev 2000 / 15274 Next

business day to monthly or quarterly aggregation.

Using zoo, convert the dates to Date class.
Then convert them to yearmon class so every
date in a month will be labelled the same.  Replace
duplicates with NAs and use locf (last observation
carried forward to fill them back in).
library(zoo)

dt <- as.Date(ourdates)
ym <- as.yearmon(dt)
isdup <- duplicated(ym)
dt[isdup] <- NA
na.locf(dt)

To do the above with quarterly data use as.yearqtr instead of as.yearmon.

In your example the first 4 points are missing from the output.  If
that was intentional
then you could retain just those months with at least 20 points, say,
by replacing last
line with:

na.locf(dt)[ ave(seq_along(dt), ym, FUN = length) >= 20 ]

Another thing to consider is it would be easy to pick out just the first date
in each month using !isdup that we calculated above if you wanted to apply
that to the values being weighted rather than repeating the weights.

For more on zoo read the two zoo vignettes:

library(zoo)
vignette("zoo")
vignette("zoo-quickref")
On Jan 10, 2008 6:14 AM, Murali Menon <feanor0 at hotmail.com> wrote: