[R] Running sum

Andy Bunn abunn at whrc.org
Fri Nov 19 19:52:24 CET 2004


see ?cumsum
x <- 1:10
cumsum(x)
max(cumsum(x))

HTH, Andy


> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Sean Davis
> Sent: Friday, November 19, 2004 1:09 PM
> To: r-help
> Subject: [R] Running sum
> 
> 
> 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?
> 
> Thanks,
> Sean
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list