I have a vector of values -1, 0, 1, say a <- c(0, 1, 0, 1, 1, -1, -1, -1, 0, -1, -1) I want to create a vector of the cumulative sum of this, but I need to set a maximum and minimum value for the cumsum, for example: max_value <- 2 min_value <- -2 the expected result would be (0, 1, 1, 2, 2, 1, 0, -1, -1, -2, -2) The only way I managed to do It, was res <- vector(length=length(a)) res[1] <- a[1] for ( i in 2:length(a)) res[i] <- res[i-1] + a[i] * (( res[i-1] < max_value & a[i] > 0 ) | ( res[i-1] > min_value & a[i] < 0 )) This is certainly not the best way to do it, so any suggestions? Henrique [[alternative HTML version deleted]]
Try this: ac <- cumsum(a) ifelse(ac > 2, max_value, ifelse(ac < -2, min_value, ac)) On Thu, Nov 25, 2010 at 6:44 PM, henrique <henrique@allianceasset.com.br>wrote:> I have a vector of values -1, 0, 1, say > > > > a <- c(0, 1, 0, 1, 1, -1, -1, -1, 0, -1, -1) > > > > I want to create a vector of the cumulative sum of this, but I need to set > a > maximum and minimum value for the cumsum, for example: > > > > max_value <- 2 > > min_value <- -2 > > > > the expected result would be (0, 1, 1, 2, 2, 1, 0, -1, -1, -2, -2) > > > > The only way I managed to do It, was > > > > res <- vector(length=length(a)) > > res[1] <- a[1] > > for ( i in 2:length(a)) res[i] <- res[i-1] + a[i] * (( res[i-1] < max_value > & a[i] > 0 ) | ( res[i-1] > min_value & a[i] < 0 )) > > > > > > This is certainly not the best way to do it, so any suggestions? > > > > > > Henrique > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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 [[alternative HTML version deleted]]
On Thu, Nov 25, 2010 at 3:44 PM, henrique <henrique at allianceasset.com.br> wrote:> I have a vector of values -1, 0, 1, say > > a <- c(0, 1, 0, 1, 1, -1, -1, -1, 0, -1, -1) > > I want to create a vector of the cumulative sum of this, but I need to set a > maximum and minimum value for the cumsum, for example: > > max_value <- 2 > min_value <- -2 > the expected result would be (0, 1, 1, 2, 2, 1, 0, -1, -1, -2, -2) >Try this: f <- function(x, y) max(min(x + y, max_value), min_value) Reduce(f, a, 0, accumulate = TRUE)[-1] -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Does this do it;> pmin(2, pmax(-2, cumsum(a)))[1] 0 1 1 2 2 2 1 0 0 -1 -2 On Thu, Nov 25, 2010 at 3:44 PM, henrique <henrique at allianceasset.com.br> wrote:> I have a vector of values -1, 0, 1, say > > > > a <- c(0, 1, 0, 1, 1, -1, -1, -1, 0, -1, -1) > > > > I want to create a vector of the cumulative sum of this, but I need to set a > maximum and minimum value for the cumsum, for example: > > > > max_value <- 2 > > min_value <- -2 > > > > the expected result would be (0, 1, 1, 2, 2, 1, 0, -1, -1, -2, -2) > > > > The only way I managed to do It, was > > > > res <- vector(length=length(a)) > > res[1] <- a[1] > > for ( i in 2:length(a)) res[i] <- res[i-1] + a[i] * (( res[i-1] < max_value > & a[i] > 0 ) | ( res[i-1] > min_value & a[i] < 0 )) > > > > > > This is certainly not the best way to do it, so any suggestions? > > > > > > Henrique > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Worked fine Gabor, thanks! I just removed the init argument from the Reduce function and I got it without the first 0. Henrique -----Mensagem original----- De: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] Enviada em: quinta-feira, 25 de novembro de 2010 22:13 Para: henrique Cc: r-help at r-project.org Assunto: Re: [R] Cumsum with a max and min value On Thu, Nov 25, 2010 at 3:44 PM, henrique <henrique at allianceasset.com.br> wrote:> I have a vector of values -1, 0, 1, say > > a <- c(0, 1, 0, 1, 1, -1, -1, -1, 0, -1, -1) > > I want to create a vector of the cumulative sum of this, but I need to seta> maximum and minimum value for the cumsum, for example: > > max_value <- 2 > min_value <- -2 > the expected result would be (0, 1, 1, 2, 2, 1, 0, -1, -1, -2, -2) >Try this: f <- function(x, y) max(min(x + y, max_value), min_value) Reduce(f, a, 0, accumulate = TRUE)[-1] -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com