merged xts object are not aligned

2019-06-28 04:19发布

问题:

please, try the following code:

library(quantmod)
getSymbols('SPY', from = '1950-01-01')
SPY <- to.monthly(SPY)
temp <- xts(Cl(SPY), index(SPY))

You will obtain a xts object which has the same length of Cl(SPY) and the same dates... or it should be so.

If you input

merge(Cl(SPY), temp)

you will see that, although Cl(SPY) and temp have the same index date, they are not aligned, the code produces doubles and a lot of NAs.

How may I merge them in a correct way?

回答1:

This has been fixed in xts on R-Forge. Please see Cannot install R-forge package using install.packages if you have issues installing xts from R-Forge.

install.packages("xts", repos="http://r-forge.r-project.org")

library(quantmod)
getSymbols('SPY', from = '1950-01-01')
SPY <- to.monthly(SPY)
temp <- xts(Cl(SPY), index(SPY))
merge(Cl(SPY),temp)


标签: r xts