I would like to add the sums of the columns of my dataframe one row at a time, conditional on another column that has a binary variable.
So for each row, I would like to compute the sum of the entire column above it for all rows where the binary variable in the corresponding row has the same value.
Here is an example:
dummy var1 var2
1 x1 y1
0 x2 y2
0 x3 y3
1 x4 y4
My goal is to obtain this:
dummy var1 var2
1 x1 y1
0 x2 y2
0 x3+x2 y3+y2
1 x4+x1 y4+y1
I have asked this question previously for a simplified version (Adding columns sums in dataframe row wise) where I just add all of the values above without the condition. Is there a way to incorporate this condition?