Sergey Goriatchev <sergeyg <at> gmail.com> writes:

> 
> Hello, everyone
> 
> I wonder if there is in R somewhere a function similar to cumsum().
> The function calculates a statistic (say mean or standard deviation)
> buy adding consequtively one more data point.
> 
> So, say I have a timeseries of 100 observations.
> I start by calculating mean of first 30 observations
> Then I add one observation and calculate mean of 31 observations
> Then I add one more observation and calculate mean of 32 observation,
> and so on until the end
> 
> Is there a function like that in R?
> 
> Best,
> Sergey
> 

  There was some recent discussion of using Reduce(...,accumulate=TRUE)
but I think this won't work in your case.  For your particular case I
think

cumsum(z)/seq_along(z) gives the running mean you want
(starting from the first obs., not the 30th, but you can always
drop the first 30).  sd() is a little harder.  You might be
able to use the (numerically inaccurate) E[z^2]-E[z]^2 trick
with cumsum ...

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to