I am adding the sums of the columns of a dataframe one row at a time, conditional on another column that has a binary variable.
So for each row, I compute the sum of the entire column above it for all rows where the binary variable in the corresponding row has the same value.
I would like to reverse this so that each row has the sum of the "wrong" group, that is the value for the incorrect dummy value (as part of a robustness test):
Here is an example:
dummy var1 var2
1 x1 y1
0 x2 y2
0 x3 y3
1 x4 y4
Currently I obtain this:
dummy var1 var2
1 x1 y1
0 x2 y2
0 x3+x2 y3+y2
1 x4+x1 y4+y1
I would like to get the wrong values, that is (first row being N/A since there is no value for dummy=0 in that row or above):
dummy var1 var2
1 N/A N/A
0 x1 y1
0 x1 y1
1 x3+x2 y3+y2
This how I did it so far (I asked this in this forum Adding columns sums in dataframe row wise conditional on a dummy) for the "correct" version:
setDT(df1)
cols = c("var1", "var2", "var3", ...)
df1[, (cols) := lapply(.SD, cummean) , by = dummy, .SD = cols]
I was thinking of just using: -dummy instead of dummy, but that does merely change the label of each group. Is there an easy way to change this?