R语言自带rollapply()函数可以达到你的要求,
比如:
在Clifford S. Ang的著作《Analyzing Financial Data and Implementing Financial Models Using R》中,就举了下列例子:
rollapply(AMZN.bb$AMZN.Close,width=20,FUN=sd,fill=NA)
来求估价布林均线的移动标准差,
你所想达到的目标应该可以用:
rollapply(a,width=5,FUN=mean,fill=NA)
完成