将新列添加到XTS对象中

Ahd*_*dee 10 r xts quantmod

嗨:我有一个xts对象:

           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2013-09-09    505.00    507.92   503.48     506.17    12116200        506.17
2013-09-10    506.20    507.45   489.50     494.64    26490200        494.64
2013-09-11    467.01    473.69   464.81     467.71    32031600        467.71
2013-09-12    468.50    475.40   466.01     472.69    14409400        472.69
Run Code Online (Sandbox Code Playgroud)

我尝试计算滚动平均值并将其附加到新列

AA["AAPL.Rolling"] <- rollmean(AA[,"AAPL.Adjusted"],12)
Run Code Online (Sandbox Code Playgroud)

虽然rollmean(AA[,"AAPL.Adjusted"],12)作品本身; 我尝试附加到新列时收到错误消息.**这也很难让新的滚动意味着每行都没有数据,因为前12个应该是"NA"任何人都可以帮忙吗?非常感谢你.

Jos*_*ich 13

您不能将列添加到zoo/xts对象中.你可以使用这个$<-功能.

AA$AAPL.Rolling <- rollmean(AA[,"AAPL.Adjusted"], 12)
Run Code Online (Sandbox Code Playgroud)

另请注意,rollmean默认情况下是居中对齐.您可能希望使用rollmeanr右对齐.填充NA将自动发生,因为您正在将滚动平均值与原始对象合并.fill=NA如果要rollmean显式添加它们,请使用.