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))]
x y q
1: 1 1 1
2: 4 5 5
3: -3 2 5
4: -2 0 5
5: 3 3 8
6: 4 7 12
What I need to do is reset q after y==0. In essence rows 1:4 belong to subgroup A and 5:6 to subgroup B. The result should be:
x y q
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 guess I could introduce another column group with values A,B,... which would change after y==0 and then use that in by expession, but I don't know how (at least not other than using for clause)
With data.table
devel version
dt[, q:=cumsum(ifelse(x>0,x,0)),by=shift(cumsum(y==0),1, fill=0)]
#library(devtools)
#install_github("Rdatatable/data.table", build_vignettes = FALSE)
# x y q
#1: 1 1 1
#2: 4 5 5
#3: -3 2 5
#4: -2 0 5
#5: 3 3 3
#6: 4 7 7
Try something like this
dt[, group:= cumsum(y == 0)]
dt[y == 0, group := group - 1]
dt[, q:=cumsum(ifelse(x>0,x,0)), by = group]
dt
# x y group q
#1: 1 1 0 1
#2: 4 5 0 5
#3: -3 2 0 5
#4: -2 0 0 5
#5: 3 3 1 3
#6: 4 7 1 7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With