0

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!

9
  • You've given us your expected output but not your input. That makes life difficult... Commented Nov 9, 2021 at 14:38
  • Thank you for your answer. I've updated the question. Commented Nov 9, 2021 at 14:44
  • To be honest, I don't quite understand your question. If we run your first chunk of code on the data you are providing, we get a column Moins_23_eleves containing only zeros (which is logical since no value is less than 23 in the columns Q16c and Q17 and that there is always a value in either of the columns). If the column contains only zeros, it means that it contains no NA. So why would you expect anything other than a value of 0 for the NA column of the dataframe generated by the table() function? Commented Nov 9, 2021 at 16:34
  • Hello Yes I understand your point. There are also instances where both Q16c and Q17 are NA. In this case I would like them to remain NAs and not turn into 0s. Commented Nov 10, 2021 at 10:37
  • O.K. thanks for the clarification. In this case, please find a possible solution with the library data.table (cf. answer below). Cheers. Commented Nov 10, 2021 at 12:28

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.