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
Running sum
18 messages · Andy Bunn, Sean Davis, Brian Ripley +8 more
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?
max(cumsum(Y)) Kjetil
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
Kjetil Halvorsen.
Peace is the most effective weapon of mass construction.
-- Mahdi Elmandjra
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
Thanks all. cumsum does the job Sean
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]
}
return(max(Y))
Is there a faster way to do this?
Thanks,
Sean
Something like:
cumsum(1:10)
[1] 1 3 6 10 15 21 28 36 45 55
max(cumsum(1:10))
[1] 55 Does that help? Marc Schwartz
?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
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 <- 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
Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567
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-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
Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/
?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) max(runSum2(a)) # To get only the max Best, Philippe ..............................................<??}))><........ ) ) ) ) ) ( ( ( ( ( Prof. Philippe Grosjean ) ) ) ) ) ( ( ( ( ( Numerical Ecology of Aquatic Systems ) ) ) ) ) Mons-Hainaut University, Pentagone ( ( ( ( ( Academie Universitaire Wallonie-Bruxelles ) ) ) ) ) 6, av du Champ de Mars, 7000 Mons, Belgium ( ( ( ( ( ) ) ) ) ) phone: + 32.65.37.34.97, fax: + 32.65.37.33.12 ( ( ( ( ( email: Philippe.Grosjean at umh.ac.be ) ) ) ) ) ( ( ( ( ( web: http://www.umh.ac.be/~econum ) ) ) ) ) ..............................................................
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] 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 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
Something like:
cumsum(1:10)
[1] 1 3 6 10 15 21 28 36 45 55
max(cumsum(1:10))
[1] 55 Does that help? Marc Schwartz
______________________________________________ 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
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]
}
return(max(Y))
Is there a faster way to do this?
My apologies, I am not being entirely serious, but... Please do read the posting guide! (particularly the third point in the "Responding to other posts" section). Bob
Bob O'Hara Department of Mathematics and Statistics P.O. Box 68 (Gustaf H??llstr??min katu 2b) FIN-00014 University of Helsinki Finland Telephone: +358-9-191 51479 Mobile: +358 50 599 0540 Fax: +358-9-191 51400 WWW: http://www.RNI.Helsinki.FI/~boh/ Journal of Negative Results - EEB: www.jnr-eeb.org
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) max(runSum2(a)) # To get only the max
Phillipe,
If you run Sean's original function for 1:10, you get:
X <- 1:10
Y <- vector(length = 10)
Y[1] <- X[1]
for (i in 2:10)
{
Y[i] <- Y[i-1] + X[i]
}
Y
[1] 1 3 6 10 15 21 28 36 45 55 which is equivalent to cumsum(1:10) Your function yields:
runSum2(1:10)
[1] 3 5 7 9 11 13 15 17 19 Which is the sum of successive individual pairs of vector elements. For future reference, the running() function in the gregmisc bundle's gtools package offers a more general approach to 'moving window' functions:
running(1:10, width = 2, fun = sum)
1:2 2:3 3:4 4:5 5:6 6:7 7:8 8:9 9:10 3 5 7 9 11 13 15 17 19 Best regards, Marc Schwartz
Perhaps I'm missing something, but isn't the maximum of the cumulative sum simply the last value, ie. sum(x)? Hadley
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 (independently) managed to over-engineer the solution. Presuming of course, that Sean does not require the entire cumulative sum vector for other purposes. I wonder what that says about human cognition.... Thanks, Marc <Cleaning his bi-focals...>
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 group of us (independently) managed to over-engineer the
Please, Please, please, ... was it ever said that the numbers was only positive? Kjetil
solution. Presuming of course, that Sean does not require the entire cumulative sum vector for other purposes. I wonder what that says about human cognition.... Thanks, Marc <Cleaning his bi-focals...>
______________________________________________ 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
Kjetil Halvorsen.
Peace is the most effective weapon of mass construction.
-- Mahdi Elmandjra
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 group of us (independently) managed to over-engineer the solution. Presuming of course, that Sean does not require the entire cumulative sum vector for other purposes. I wonder what that says about human cognition.... Thanks, Marc <Cleaning his bi-focals...>
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 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:00 -0400, Kjetil Brinchmann Halvorsen wrote:
Please, Please, please, ... was it ever said that the numbers was only positive?
Thanks to both Doug and Kjetil. I stand corrected. Marc
Ooops! Sorry. I did not look carefully enough on the code and focused on the term! My proposition is for a "running sum with a window width of 2 observations" (the title of the question is "Running sum", isn't it?), while the true question was indeed about a *cumulative sum*, which is something totally different. Best, Philippe Grosjean
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Philippe Grosjean Sent: Friday, November 19, 2004 9:11 PM To: MSchwartz at MedAnalytics.com; 'Sean Davis' Cc: 'R-Help' Subject: RE: [R] Running sum ?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) max(runSum2(a)) # To get only the max Best, Philippe ..............................................<??}))><........ ) ) ) ) ) ( ( ( ( ( Prof. Philippe Grosjean ) ) ) ) ) ( ( ( ( ( Numerical Ecology of Aquatic Systems ) ) ) ) ) Mons-Hainaut University, Pentagone ( ( ( ( ( Academie Universitaire Wallonie-Bruxelles ) ) ) ) ) 6, av du Champ de Mars, 7000 Mons, Belgium ( ( ( ( ( ) ) ) ) ) phone: + 32.65.37.34.97, fax: + 32.65.37.33.12 ( ( ( ( ( email: Philippe.Grosjean at umh.ac.be ) ) ) ) ) ( ( ( ( ( web: http://www.umh.ac.be/~econum ) ) ) ) ) ..............................................................
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] 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 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
Something like:
cumsum(1:10)
[1] 1 3 6 10 15 21 28 36 45 55
max(cumsum(1:10))
[1] 55 Does that help? Marc Schwartz
______________________________________________ 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
______________________________________________ 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
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