Re: [R] Cumsum with a max and min value

2010-11-25 Thread jim holtman
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 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,

Re: [R] Cumsum with a max and min value

2010-11-25 Thread Gabor Grothendieck
On Thu, Nov 25, 2010 at 3:44 PM, henrique 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 >

Re: [R] Cumsum with a max and min value

2010-11-25 Thread Henrique Dallazuanna
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 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, bu

[R] Cumsum with a max and min value

2010-11-25 Thread henrique
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