Consider the following named vector x.
x
( x <- setNames(c(1, 2, 0, NA, 4, NA, NA, 6), letters[1:8]) ) # a b c d e f g h # 1 2 0 NA
It's an old question but tidyr gives a new solution. Based on the idea of replacing NA with zero.
tidyr
NA
require(tidyr) cumsum(replace_na(x, 0)) a b c d e f g h 1 3 3 3 7 7 7 13