Max Max - 1 year ago 118
R Question

r cumsum differentiate reset at >0

I am quite new to R and tried some formulas form the forum but somehow I could not modify them to meet my condition.
I want to construct a cumsum which starts new when the variable in column E is >=0:

NR E Proability Cumsum
1 -.2 .2 .2
2 -.15 .2 .4
3 -.1 .2 .6
4 .1 .2 .2
5 .2 .2 .4

The last column should be the result.

Does anyone have an idea?

Thank you very much :)

Answer Source

There's a very useful function called rleid in the data.table package which you can make use of for the grouping. I'll use it in combination with some base functions:

df <- transform(df, cumsum2 = ave(Proability, rleid(E > 0), FUN = cumsum))

Or, if you want to fully use data.table functions, it would be

df[, cumsum2 := cumsum(Proability), by = rleid(E > 0)]
setDF(df) # optional
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download