I'm trying to convert a daily series into a weekly one ending on Fridays. There are missing values for each series and therefore merge
function leave some NAs. I have tried with na.locf
and to.weekly
but didn't work for me. I'm creating a weekly date object containg all fridays in that period and, because some weeks end on wednesdays or thursdays for some series, I can't match those indexes.
Ideally I would like to overwrite the date of the last value in those weeks not ending on fridays.
library(quantmod)
library(xts)
TickerL <- c("ABE.MC", "FNC.MI", "ENI.MI")
getSymbols(TickerL,from = "2000-01-01")
pr <- list(ABE.MC[,4], FNC.MI[,4], ENI.MI[,4])
w.dates <- seq(from=min(index(ABE.MC)),to=max(index(ABE.MC)), by='days')
w.dates <- w.dates[.indexwday(as.xts(w.dates))==5]
if (max(index(ABE.MC)) > max(w.dates)) w.dates <- c(w.dates,seq(last(w.dates),by='weeks',length.out=2)[2])
pr2 <- lapply(pr, function(x) do.call(rbind, lapply(split(x, "weeks"), last)))
pr3 <- do.call(merge, pr2)
Thanks to @G. Grothendieck the function
nextfri
in the zoo package vignette did the trick!