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?
CodePudding user response:
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.nafunction
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))
CodePudding user response:
OKAY! I found the answer. I used
ECEMD$PROFSKILL_PROFPRACTICE_INDICE2[is.na(ECEMD$PROFSKILL_PROFPRACTICE_INDICE2)] <- 0
