I have a few years of daily price data as a zoo object. What's the best way to subset the last week of 'every' month in R?
Here's how you can replicate it:
set.seed(123)
price <- rnorm(365)
data <- cbind(seq(as.Date("2013-01-01"), by = "day", length.out = 365), price)
zoodata <- zoo(data[,2], as.Date(data[,1]))
I tried this option but it only returns the last week of the entire data set.
do.call(rbind, lapply(split(zoodata, "months"), last, "1 week"))
Resulting output is:
2013-12-30 2013-12-31
1.0246732 0.8176594
I'm hoping to get the last week of every month so there should be at least 12 weeks of data.
Thanks in advance!