Home > Mobile >  Issues with multiple nested if-else in R
Issues with multiple nested if-else in R

Time:11-17

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!

CodePudding user response:

I suggest you a solution with the package data.table. So please find the reprex below.

Reprex

  • Your modified data (to get all cases)
m <- "Q16a  Q16c    Q17
2   NA  31
2   NA  28
1   NA  NA
1   29  NA
1   22  NA
1   25  NA
1   25  NA"

Ech_final_nom_BSA <- read.table(text = m, header = TRUE)
  • Code
library(data.table)

Ech_final_nom_BSA <- setDT(Ech_final_nom_BSA)[, Moins_23_eleves := fcase(Q16a == 1 & Q16c < 23 | Q16a == 1 & Q17 < 23, 1,
                                                                         Q16a == 1 & Q16c >= 23 | Q16a == 1 &Q17 >= 23, 0,
                                                                         default = NA)][]
  • Output
Ech_final_nom_BSA 
#>    Q16a Q16c Q17 Moins_23_eleves
#> 1:    2   NA  31              NA
#> 2:    2   NA  28              NA
#> 3:    1   NA  NA              NA
#> 4:    1   29  NA               0
#> 5:    1   22  NA               1
#> 6:    1   25  NA               0
#> 7:    1   25  NA               0
  • Check with table()
table(factor(Ech_final_nom_BSA$Moins_23_eleves, levels = 0:1) , useNA = "always")
#> 
#>    0    1 <NA> 
#>    3    1    3

Created on 2021-11-10 by the reprex package (v2.0.1)


EDIT

Solution with the dplyr library

Reprex

  • Your modified data (to get all cases)
m <- "Q16a  Q16c    Q17
2   NA  31
2   NA  28
1   NA  NA
1   29  NA
1   22  NA
1   25  NA
1   25  NA"
Ech_final_nom_BSA <- read.table(text = m, header = TRUE)
  • Code
library(dplyr)

Ech_final_nom_BSA <- Ech_final_nom_BSA %>%
  mutate(Moins_23_eleves = case_when(Q16a==1 & Q16c <= 22 | Q16a == 1 & Q17 <= 22 ~ 1,
                                     Q16a==1 & Q16c > 22 | Q16a == 1 & Q17 > 22 ~ 0)
         )
  • Output
Ech_final_nom_BSA 
#>   Q16a Q16c Q17 Moins_23_eleves
#> 1    2   NA  31              NA
#> 2    2   NA  28              NA
#> 3    1   NA  NA              NA
#> 4    1   29  NA               0
#> 5    1   22  NA               1
#> 6    1   25  NA               0
#> 7    1   25  NA               0
  • Check with table()
table(factor(Ech_final_nom_BSA$Moins_23_eleves, levels = 0:1) , useNA = "always")
#> 
#>    0    1 <NA> 
#>    3    1    3

Created on 2021-11-10 by the reprex package (v2.0.1)

  • Related