lundi 25 avril 2016

R: if function for dublicate rows

I have a dataframe like below:

> df
     pat_id disease
[1,] "pat1" "dis1" 
[2,] "pat1" "dis1" 
[3,] "pat2" "dis0" 
[4,] "pat2" "dis5" 
[5,] "pat3" "dis2" 
[6,] "pat3" "dis2" 

How can I write a function to get a third variable which indicates for the same pat_id the disease variable is the same or not , like below?

> df
     pat_id disease var3
[1,] "pat1" "dis1"  "1" 
[2,] "pat1" "dis1"  "1" 
[3,] "pat2" "dis0"  "0" 
[4,] "pat2" "dis5"  "0" 
[5,] "pat3" "dis2"  "1" 
[6,] "pat3" "dis2"  "1" 

Thanks in advance for any help...

Aucun commentaire:

Enregistrer un commentaire