I have this vector :
x = c(1,1,1,1,1,0,1,0,0,0,1,1)
And I want to do a cumulative sum for the positive numbers only. I should have the fol
x<-c(1,1,1,1,1,0,1,0,0,0,1,1) skumulowana<-function(x) { dl<-length(x) xx<-numeric(dl+1) for (i in 1:dl){ ifelse (x[i]==0,xx[i+1]<-0,xx[i+1]<-xx[i]+x[i]) } wynik<<-xx[1:dl+1] return (wynik) } skumulowana(x) ## [1] 1 2 3 4 5 0 1 0 0 0 1 2