问题
(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