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
答案 0 :(得分:1)
您正在寻找的是merge
功能。像
merge(bondIndex[,1], bondIndex[,2], all = TRUE)