mardi 11 décembre 2018

R compare two columns return third column if any column match conditions

I have a dataset:data1 which looks like this: ME PDR 1 2
NA 1
NA 1
1 2
NA NA

I want to get this:

ME PDR case
1   2   2
NA  1   1
NA  1   1
1   2   2
NA  NA  NA

I tried to use this command but it doesn't return me 1 when I have 1 in either columns and no 2 in any of them.

data1$case=ifelse(data1$ME==2 | data1$PDR==2 ,2,ifelse(data1$ME==NA & data1$PDR==NA,NA,1))

Aucun commentaire:

Enregistrer un commentaire