5

(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

mommomonthewind
  • 4,390
  • 11
  • 46
  • 74

1 Answers1

11

Try this

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

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

Kalidoss M
  • 556
  • 8
  • 15