0

I want to create a binary variable about people being interested in professional practices. I did so with the different options, but I can't seem to find a way to attribute the value 0 to observations. Does someone know how?

ECEMD <-
  ECEMD %>%
  mutate(PROFSKILL_PROFPRACTICE_INDICE2 = case_when(
    PROFSKILL_PROFPRACTICE_DIM == "Did not participate" ~ 0,
    PROFSKILL_PROFPRACTICE_DIM == "Not applicable" ~ 0,
    PROFSKILL_PROFPRACTICE_DIM == "Excellent" ~ 1,
    PROFSKILL_PROFPRACTICE_DIM == "Poor" ~ 1,
    PROFSKILL_PROFPRACTICE_DIM == "Fair" ~ 1,
    PROFSKILL_PROFPRACTICE_DIM == "Good" ~ 1,
    PROFSKILL_PROFPRACTICE_DIM == "Very good" ~ 1, 
    PROFSKILL_PROFPRACTICE_DIM == NA ~ 0))
freq(ECEMD$PROFSKILL_PROFPRACTICE_INDICE2)

UPDATE:

Okay, I tried

ECEMD <-
  ECEMD %>%
  mutate(PROFSKILL_PROFPRACTICE_INDICE2 = case_when(
    PROFSKILL_PROFPRACTICE_DIM %in% c("Did not participate",
                                       "Not applicable",
                                       NA) ~ 0, 
    PROFSKILL_PROFPRACTICE_DIM %in% c("Excellent",
                                          "Poor",
                                          "Fair",
                                          "Good",
                                          "Very good") ~ 1))

But the NA are still not changed to 0. What do?

FALA
  • 11
  • 3
  • Can't use `==` to detect `NA` values because they may or may not be equal. Use `is.na()`. – Gregor Thomas Jan 05 '22 at 15:28
  • An alternative to the canonical `is.na(.)` in the answer below is to replace `==` with `%in%`, which does allow `NA`. (It is not as efficient, certainly, but it provides a different path, that's all.) – r2evans Jan 05 '22 at 15:30
  • in order to do any more debugging we're going to need a [mcve]. See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ... – Ben Bolker Jan 05 '22 at 16:04

2 Answers2

1

probably

is.na(PROFSKILL_PROFPRACTICE_DIM) ~ 0

(a reproducible example would be nice ...)

NA handling is in general very tricky. You might even want to put this clause first, so that all the equality clauses don't get messed up by NA values (because x == NA is NA, not TRUE or FALSE, for all values ...)

The fourth example in ?case_when actually discusses this issue explicitly:

Note that NA values in the vector x do not get special treatment. If you want to explicitly handle NA values you can use the is.na function

As mentioned by @r2evans, using %in% could work too:

mutate(PROFSKILL_PROFPRACTICE_INDICE2 = case_when(
    PROFSKILL_PROFPRACTICE_DIM %in% c("Did not participate",
                                      "Not applicable",
                                      NA) ~ 0,
    PROFSKILL_PROFPRACTICE_DIM %in% c("Excellent" ,
                                      "Poor",
                                      "Fair",
                                      "Good",
                                      "Very good") ~ 1
   )

or perhaps even

mutate(PROFSKILL_PROFPRACTICE_INDICE2 =  
     ifelse(PROFSKILL_PROFPRACTICE_DIM %in% c("Did not participate",
                                              "Not applicable",
                                              NA),
    0, 1))
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • When I tried ECEMD <- ECEMD %>% is.na(PROFSKILL_PROFPRACTICE_INDICE2) ~ 0 freq(ECEMD$PROFSKILL_PROFPRACTICE_INDICE2) It says : Error in freq(ECEMD$PROFSKILL_PROFPRACTICE_INDICE2) : ECEMD$PROFSKILL_PROFPRACTICE_INDICE2 is either NULL or does not exist – FALA Jan 05 '22 at 15:39
  • it doesn't look like you included the `mutate()` ? my first suggestion is meant to be incorporated into your `case_when` statement, not as a standalone solution – Ben Bolker Jan 05 '22 at 15:49
  • I did try with %in% now, but still won't change. Also, I am not sure how to integrate mutate() with the formula you gave with (is.na) – FALA Jan 05 '22 at 16:01
0

OKAY! I found the answer. I used

ECEMD$PROFSKILL_PROFPRACTICE_INDICE2[is.na(ECEMD$PROFSKILL_PROFPRACTICE_INDICE2)] <- 0
FALA
  • 11
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 05 '22 at 18:47