You can't add columns to zoo/xts objects like that. You can use the $<-
function though.
AA$AAPL.Rolling <- rollmean(AA[,"AAPL.Adjusted"], 12)
Also note that rollmean
is center-aligned by default. You may want to use rollmeanr
to get right-alignment. Padding with NA
will happen automatically, since you're merging the rolling mean with the original object. Use fill=NA
if you want rollmean
to add them explicitly.