2012-09-06 104 views
4

請嘗試以下代碼:合併XTS對象不對齊

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

您將獲得xts對象,它具有的Cl(SPY)相同的長度和相同的日期...或者應該如此。

如果你輸入

merge(Cl(SPY), temp) 

,你會看到,雖然Cl(SPY)temp具有相同的索引到目前爲止,他們沒有對齊,代碼生成雙打和很多NA秒。

我該如何以正確的方式合併它們?

回答

5

這已在R-Forge的xts中修復。如果您在安裝R-Forge的xts時遇到問題,請參閱Cannot install R-forge package using install.packages

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) 
+4

+1。更新是最好的解決方案,但是解決方法是使用'drop.time = TRUE'。即'SPY < - 月(SPY,drop.time = TRUE)' – GSee