How to combine xts datasets with slightly different dates

时间:2015-11-18 21:03:56

标签: r xts quantmod

I've been working on a financial model using data from several sources, like Yahoo and FRED via quantmod, which returns an xts datatype.

I'm able to get a dataset from Yahoo, no problem. I've also managed to add things like 52 week hi/low and moving averages as columns in the dataset. My question now is, how do I add a whole new dataset? So in addition to all my columns, I want to add a column using interest rates from FRED. The major problem is the dates are slightly different as bond and stock markets have different holidays. What's the best way to combine these datasets? I don't mind and am interested in using na.spline() to fill in the missing data.

require(quantmod)

fiveYearsAgo = Sys.Date() - (365 * 5) # This is not exactly five years

bondIndex <- getSymbols("LQD",src="google",from = fiveYearsAgo, auto.assign = FALSE)[,c(0,4)]

print (bondIndex[,1]) # works

bondIndex$fedFund <- na.spline(getSymbols("DFF", src = "FRED", from = fiveYearsAgo, auto.assign = FALSE),na.rm = TRUE)

print (bondIndex[,2]) #Has a trailing NA

print (bondIndex) #Has a mix of NA

1 个答案:

答案 0 :(得分:1)

您正在寻找的是merge功能。像

这样的东西
merge(bondIndex[,1], bondIndex[,2], all = TRUE)