I am having trouble with the code below and I am not sure why it does not work.
| Q16a | Q16c | Q17 |
|---|---|---|
| 2 | NA | 31 |
| 2 | NA | 28 |
| 1 | 26 | NA |
| 1 | 29 | NA |
| 1 | 32 | NA |
| 1 | 25 | NA |
| 1 | 25 | NA |
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!
Moins_23_elevescontaining only zeros (which is logical since no value is less than 23 in the columnsQ16candQ17and that there is always a value in either of the columns). If the column contains only zeros, it means that it contains noNA. So why would you expect anything other than a value of 0 for the NA column of thedataframegenerated by thetable()function?data.table(cf. answer below). Cheers.