mardi 9 novembre 2021

Issues with multiple nested if-else in R

I am having trouble with the code below and I am not sure why it does not work.

Ech_final_nom_BSA <- Ech_final_nom_BSA %>%
  mutate(Moins_23_eleves = ifelse(Q16a==1,
                                  ifelse(!is.na(Q16c),ifelse(Q16c<=22,1,0),
                                          NA),
                                          ifelse(!is.na(Q17),ifelse(Q17<=22,1,0),NA
                                              )))

As a result I would like the variable Moins_23_eleves to be equal 1 when Q17 or Q16c is below 23 but I don't want NA values to equal 0. The code above works but it still considers NAs as 0.

table(Ech_final_nom_BSA$Moins_23_eleves, useNA = "always")
0 1 NA
1076 597 0

What am I doing wrong?

Thanks!

Aucun commentaire:

Enregistrer un commentaire