[R] Conditional Sum

Bert Jacobs b.jacobs at pandora.be
Tue Feb 6 16:59:42 CET 2007


Hi,

I would like to make a conditional sum for certain columns in a dataframe.

This is how my dataframe looks like:

Clinic Rep ColM1 ColM2 ColM3 ... ColM40
A      1     1    0      0        1
B      1     0    -1     0        0
C      1     0    -1     1        -1 
A      2     1    1      -1       0
B      2     -1   0      0        1 
C      2     1    0      1       -1

I would like to have two new dataframes so that
Dataframe1: with the count of all 1

Clinic  ColM1  ColM2 ColM3 .. ColM40
A        2      1    0         1
B        0      0    0         1
C        1      0    2         0

Dataframe2: with the count of all -1

Clinic  ColM1  ColM2 ColM3 .. ColM40
A        0      0    1         0
B        1      1    0         0
C        0      1    0         2


Is there an easy way to achieve this.
Thx,
Bert



More information about the R-help mailing list