Re: [R] Running sum

2004-11-20 Thread hadley wickham
> Perhaps I'm missing something, but isn't the maximum of the cumulative > sum simply the last value, ie. sum(x)? As many have mentioned, I was forgetting the negative numbers. Thanks to those who pointed that out. Hadley __ [EMAIL PROTECTED] mailing

RE: [R] Running sum

2004-11-20 Thread Philippe Grosjean
sum*, which is something totally different. Best, Philippe Grosjean > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of > Philippe Grosjean > Sent: Friday, November 19, 2004 9:11 PM > To: [EMAIL PROTECTED]; 'Sean Davis' >

Re: [R] Running sum

2004-11-19 Thread Marc Schwartz
On Fri, 2004-11-19 at 18:00 -0600, Douglas Bates wrote: > I have forgotten the details of the original question but it seems to me > that if the elements of x could be both positive and negative then the > maximum of the cumulative sum doesn't have to be the last sum. On Fri, 2004-11-19 at 20:0

Re: [R] Running sum

2004-11-19 Thread Douglas Bates
Marc Schwartz wrote: On Fri, 2004-11-19 at 16:44 -0600, hadley wickham wrote: Perhaps I'm missing something, but isn't the maximum of the cumulative sum simply the last value, ie. sum(x)? Hadley Indeed! And...going back to Sean's original post he did write: "I just need max(Y)." Thus, a whole grou

Re: [R] Running sum

2004-11-19 Thread Kjetil Brinchmann Halvorsen
Marc Schwartz wrote: On Fri, 2004-11-19 at 16:44 -0600, hadley wickham wrote: Perhaps I'm missing something, but isn't the maximum of the cumulative sum simply the last value, ie. sum(x)? Hadley Indeed! And...going back to Sean's original post he did write: "I just need max(Y)." Thus, a wh

Re: [R] Running sum

2004-11-19 Thread Marc Schwartz
On Fri, 2004-11-19 at 16:44 -0600, hadley wickham wrote: > Perhaps I'm missing something, but isn't the maximum of the cumulative > sum simply the last value, ie. sum(x)? > > Hadley Indeed! And...going back to Sean's original post he did write: "I just need max(Y)." Thus, a whole group of us

Re: [R] Running sum

2004-11-19 Thread hadley wickham
Perhaps I'm missing something, but isn't the maximum of the cumulative sum simply the last value, ie. sum(x)? Hadley __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.or

RE: [R] Running sum

2004-11-19 Thread Marc Schwartz
On Fri, 2004-11-19 at 21:10 +0100, Philippe Grosjean wrote: > ?cumsum is not exactly the answer (as I understand it), but a part of it. > I propose: > > runSum2 <- function(x) > cumsum(x)[-1] - c(0, cumsum(x[1:(length(x) - 2)])) > > # Example > a <- round(runif(10, 0, 10)) > a > runSum2(a)

Re: [R] Running sum

2004-11-19 Thread Anon.
Prof Brian Ripley wrote: ?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]

RE: [R] Running sum

2004-11-19 Thread Philippe Grosjean
--Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Marc Schwartz > Sent: Friday, November 19, 2004 7:57 PM > To: Sean Davis > Cc: R-Help > Subject: Re: [R] Running sum > > On Fri, 2004-11-19 at 13:08 -0500, Sean Davis wrote: > > I

Re: [R] Running sum

2004-11-19 Thread Roger D. Peng
You could try using embed(), but I doubt it's faster. -roger 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

Re: [R] Running sum

2004-11-19 Thread Spencer Graves
Have you considered "cumsum"? > cumsum(c(1, 2, 3, -9, 2)) [1] 1 3 6 -3 -1 hope this helps. spencer graves 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 <- v

Re: [R] Running sum

2004-11-19 Thread Prof Brian Ripley
?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 th

Re: [R] Running sum

2004-11-19 Thread Marc Schwartz
On Fri, 2004-11-19 at 13:08 -0500, 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]

Re: [R] Running sum

2004-11-19 Thread Sean Davis
Thanks all. cumsum does the job Sean __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

RE: [R] Running sum

2004-11-19 Thread Andy Bunn
see ?cumsum x <- 1:10 cumsum(x) max(cumsum(x)) HTH, Andy > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] 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 ha

RE: [R] Running sum

2004-11-19 Thread Liaw, Andy
See cumsum: > x <- rnorm(10) > cs <- function(X) { + N <- length(X) + Y <- vector(length=N) + Y[1] <- X[1] + for (i in 2:N) { +Y[i] <- Y[i-1]+X[i] + } + return(max(Y)) + } > cs(x) [1] 3.228554 > max(cumsum(x)) [1] 3.228554 Andy > From: Sean Davis > > I hav

Re: [R] Running sum

2004-11-19 Thread Kjetil Brinchmann Halvorsen
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