You can use the ifelse
also:
enem$TP_COR_RACA <- ifelse(enem$TP_COR_RACA == "Nao", NA, enem$TP_COR_RACA)
The ifelse
takes three arguments:
- A comparison:
enem$TP_COR_RACA == "Nao"
- Result if comparison is true: NA
- Result if false: Enem$TP_COR_RACA (the vector value itself)
It will make the comparison for each element of the vector.
Another way is the function recode_factor
of dplyr
.
Example:
> a <- factor(c("a", "b", "c", "Não", "a"))
> recode_factor(a, `Não` = NA_character_)
[1] a b c <NA> a
Levels: a b c