How to calculate cumulative mean in R? [duplicate]
问题 This question already has answers here : Calculate cumulative average (mean) (7 answers) Closed 3 years ago . (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:/