Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.table, how to reset cumsum (or add a group to group by)

Tags:

r

data.table

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)

like image 734
zapp0 Avatar asked Apr 13 '15 11:04

zapp0


2 Answers

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
like image 189
ExperimenteR Avatar answered Nov 02 '22 18:11

ExperimenteR


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
like image 33
konvas Avatar answered Nov 02 '22 17:11

konvas