[R] Function similar to cumsum/cumprod

Sergey Goriatchev sergeyg at gmail.com
Tue Sep 22 16:36:48 CEST 2009


Henrique, thank you!

On Tue, Sep 22, 2009 at 16:32, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> Try this;
>
> set.seed(123)
> x <- rnorm(100)
> sapply(30:length(x), function(i)mean(x[1:i]))
>
> On Tue, Sep 22, 2009 at 11:12 AM, Sergey Goriatchev <sergeyg at gmail.com> wrote:
>> 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
>>
>> --
>> Kniven skärpes bara mot stenen.
>>
>> ______________________________________________
>> R-help at 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.
>>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>



-- 
I'm not young enough to know everything. /Oscar Wilde
Experience is one thing you can't get for nothing. /Oscar Wilde
When you are finished changing, you're finished. /Benjamin Franklin
Tell me and I forget, teach me and I remember, involve me and I learn.
/Benjamin Franklin
Luck is where preparation meets opportunity. /George Patten

Kniven skärpes bara mot stenen.




More information about the R-help mailing list