mardi 8 août 2017

How to select /count rows in a column based on multiple conditions

I have a data frame (1 millions of data) that looks like that : (the treatment has multiple possibily of character variable, I just simplified for the question)

ID              Position            Treatment
--20AxECvv-         0           A
--20AxECvv-         -1          A
--20AxECvv-         -2          A
--h9INKewQf-        0           A
--h9INKewQf-        -1          B
zZU7a@8jN           0           B
QUeSNEXmdB          0           C
QUeSNEXmdB          -1          C
qu72Ql@h79          0           C

I just want to keep the ID with exclusif treatment, in other word keep ID who was treated by only one treatment even if it was several times. After, I want to sum the number of ID for each treatment. The result would be :

ID              Position            Treatment
--20AxECvv-         0           A
--20AxECvv-         -1          A
--20AxECvv-         -2          A
zZU7a@8jN           0           B
QUeSNEXmdB          0           C
QUeSNEXmdB          -1          C   
qu72Ql@h79          0           C

And the sum :
A : 1 
B : 1
C : 2

I have any ida how to resolve this, maybe with a loop within a loop but I am a beginner with R.

Aucun commentaire:

Enregistrer un commentaire