I have a data table with some computed columns
dt <- data.table(x=c(1,4,-3,-2,3,4)) dt[,y:=cumsum(x)] dt[,q:=cumsum(ifelse(x>0,x,0))] xyq 1: 1 1 1 2: 4 5 5 3: -3 2 5 4: -2 0 5 5: 3 3 8 6: 4 7 12
I need to reset q after y == 0. Essentially lines 1: 4 belong to subgroup A and 5: 6 to subgroup B. The result should be:
xyq 1: 1 1 1 2: 4 5 5 3: -3 2 5 4: -2 0 5 5: 3 3 3 6: 4 7 7
I assume that I could imagine another group of columns with values A, B, ... that would change after y == 0, and then use this by exception, but I don't know how (at least not using for suggestions)
r data.table
zapp0
source share