29

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 following vector in return:

xc = (1,2,3,4,5,0,1,0,0,0,1,2)

How could I do it?

I've tried : cumsum(x) but that do the cumulative sum for all values and gives :

cumsum(x)
[1] 1 2 3 4 5 5 6 6 6 6 7 8
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
Math
  • 1,274
  • 3
  • 14
  • 32

9 Answers9

30

One option is

x1 <- inverse.rle(within.list(rle(x), values[!!values] <- 
                  (cumsum(values))[!!values]))
x[x1!=0] <- ave(x[x1!=0], x1[x1!=0], FUN=seq_along)
x
#[1] 1 2 3 4 5 0 1 0 0 0 1 2

Or a one-line code would be

 x[x>0] <-  with(rle(x), sequence(lengths[!!values]))
 x
 #[1] 1 2 3 4 5 0 1 0 0 0 1 2
akrun
  • 874,273
  • 37
  • 540
  • 662
20

Here's a possible solution using data.table v >= 1.9.5 and its new rleid funciton

library(data.table)
as.data.table(x)[, cumsum(x), rleid(x)]$V1
## [1] 1 2 3 4 5 0 1 0 0 0 1 2
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
10

Base R, one line solution with Map Reduce :

> Reduce('c', Map(function(u,v) if(v==0) rep(0,u) else 1:u, rle(x)$lengths, rle(x)$values))
 [1] 1 2 3 4 5 0 1 0 0 0 1 2

Or:

unlist(Map(function(u,v) if(v==0) rep(0,u) else 1:u, rle(x)$lengths, rle(x)$values))
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87
5
x=c(1,1,1,1,1,0,1,0,0,0,1,1)
cumsum_ <- function(x) {
  r <- rle(x)
  s <- split(x, rep(seq_along(r$values), rle(x)$lengths))
  return(unlist(sapply(s, cumsum), use.names = F))
}
(xc <- cumsum_(x))
# [1] 1 2 3 4 5 0 1 0 0 0 1 2
lukeA
  • 53,097
  • 5
  • 97
  • 100
3

I dont know much of R but i have written a small code in Python. Logic remains the same in all language. Hope this will help you

x=[1,1,1,1,1,0,1,0,0,0,1,1]
tot=0
for i in range(0,len(x)):
    if x[i]!=0:
        tot=tot+x[i]
        x[i]=tot
    else:
        tot=0
print x
  • 4
    If you use this it would be better (i.e. more efficient) to translate it to C++ and use Rcpp. – Roland Feb 03 '15 at 11:57
2
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
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
2

Try this one-liner...

Reduce(function(x,y) (x+y)*(y!=0), x, accumulate=T)
sirallen
  • 1,947
  • 14
  • 21
1

split and lapply version:

x <- c(1,1,1,1,1,0,1,0,0,0,1,1)
unlist(lapply(split(x, cumsum(x==0)), cumsum))

step by step:

a <- split(x, cumsum(x==0)) # divides x into pieces where each 0 starts a new piece
b <- lapply(a, cumsum)  # calculates cumsum in each piece
unlist(b)  # rejoins the pieces 

Result has useless names but is otherwise what you wanted:

# 01 02 03 04 05 11 12  2  3 41 42 43 
#  1  2  3  4  5  0  1  0  0  0  1  2 
lebatsnok
  • 6,329
  • 2
  • 21
  • 22
0

Here is another base R solution using aggregate. The idea is to make a data frame with x and a new column named x.1 by which we can apply aggregate functions (cumsum in this case):

x <- c(1,1,1,1,1,0,1,0,0,0,1,1)
r <- rle(x)
df <- data.frame(x, 
x.1=unlist(sapply(1:length(r$lengths), function(i) rep(i, r$lengths[i]))))

# df

   # x x.1
# 1  1   1
# 2  1   1
# 3  1   1
# 4  1   1
# 5  1   1
# 6  0   2
# 7  1   3
# 8  0   4
# 9  0   4
# 10 0   4
# 11 1   5
# 12 1   5

agg <- aggregate(df$x~df$x.1, df, cumsum)
as.vector(unlist(agg$`df$x`))

# [1] 1 2 3 4 5 0 1 0 0 0 1 2
989
  • 12,579
  • 5
  • 31
  • 53