Search code examples
rmergextsapply

How to align dates for merging two xts files?


I'm trying to analyze 1-year %-change data in R on two data series by merging them into one file. One series is weekly and the other is monthly. Converting the weekly series to monthly is the problem. Using apply.monthly() on the weekly data creates a monthly file but with intra-monthly dates that don't match the first-day-of-month format in the monthly series after combining the two files via merge.xts(). Question: How to change the resulting merged file (sample below) to one monthly entry for both series?

2012-11-01 0.02079801          NA
2012-11-24         NA -0.03375796
2012-12-01 0.02052502          NA
2012-12-29         NA  0.04442094
2013-01-01 0.01881466          NA
2013-01-26         NA  0.06370272
2013-02-01 0.01859883          NA
2013-02-23         NA  0.02999318

Solution

  • You can pass indexAt="firstof" in a call to to.monthly to get monthly data using the first of the month for the index.

    library(quantmod) 
    getSymbols(c("USPRIV", "ICSA"), src="FRED")
    merge(USPRIV, to.monthly(ICSA, indexAt="firstof", OHLC=FALSE))