mardi 3 mars 2020

Combine several multiple binary variables into 1 in R

Here is the story:

I have a data set with many binary variables (1 = yes and 0 = no). The thing is that many of those variables are the same, but just written differently. Example:

  • twins
    • gemelli
    • DCDA
    • MCDA
    • twin DCDA
    • twin MCDA
    • ...

It all depends on the doctor/his habits/his mood/his literacy. If an observation gets a "1" for any of those above variables, it means that there will be twins (pregnancy). Now to make some predictions and stuff about twins, I need to group all those observations that have a "1" in any of those possible variables (sometimes even in 2 of them).

Here is what I tried:

features <- mutate(features,
                   TWIN_P = ifelse("twins" == 1 |
                                      "gemelli" == 1 |
                                      "DCDA" == 1 |
                                      "MCDA" == 1 |
                                      "twin DCDA" == 1 |
                                      "twin MCDA" == 1 , 
                                      "1", "0"))

But when I look at the new variable TWIN_P I get 0 twins... Which is of course impossible.

Can someone tell me what I'm doing wrong? The binary variables are numerical. I tried to do this in between "" or without them. But nothing really worked.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire