How to calculate cumulative mean in R? [duplicate]

不问归期 提交于 2021-02-16 15:19:06

问题


(I am sorry if the term is not correct).

In R, I have a numeric vector x. I want to create new vector y where:

y[i] = mean (x[1:i)

It is easy to write a function to calculate y, but is there a built-in function in R which do the task?

Thank you very much


回答1:


Try this

y <- cumsum(x) / seq_along(x) 

Reference https://stat.ethz.ch/pipermail/r-help/2008-May/162729.html



来源:https://stackoverflow.com/questions/44757204/how-to-calculate-cumulative-mean-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!