Andrew
Andrew

Reputation: 37

How to combine xts datasets with slightly different dates

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

Upvotes: 0

Views: 466

Answers (1)

romants
romants

Reputation: 3648

What you are looking for is merge function. Something like

merge(bondIndex[,1], bondIndex[,2], all = TRUE)

Upvotes: 1

Related Questions