[R] Running sum

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Nov 19 19:58:14 CET 2004


?cumsum

On Fri, 19 Nov 2004, Sean Davis wrote:

> I have vector X of length N that I want to have a running sum for (called Y). 
> I just need max(Y).  I do this with a "for" loop like so:
>
>    Y <- vector(length=N)
>    Y[1] <- X[1]
>    for (i in 2:N) {
>      Y[i] <- Y[i-1]+X[i]
>    }
>    return(max(Y))
>
> Is there a faster way to do this?

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list