I have a data frame:
Y  X1  X2  X3
1   1   0  1
1   0   1  1
0   1   0  1
0   0   0  1
1   1   1  0
0   1   1  0
I want sum over all rows in Y column based on other columns that equal to 1, which is sum(Y=1|Xi =1). For example, for column X1, s1 = sum(Y=1|Xi =1) =1 + 0 +1+0 =2
Y  X1   
1   1   
0   1    
1   1    
0   1   
For X2 column, the s2 = sum(Y=1|Xi =1) = 0 +1+0 =1
    Y   X2  
    0   1   
    1   1    
    0   1     
For X3 column, the s3 = sum(Y=1|Xi =1) = 1+1 +0+0 =2
    Y    X3
    1    1
    1    1
    0    1
    0    1
I have a rough idea to use apply(df, 2, sum) for the column of the dataframe, but I have no idea how to subset each column based on Xi, then calculate the sum of Y. Any help is appreciated!
 
     
     
    