1

I've got an issue with recode_factor function. For some reason, I can't replace NAs with 'missing'.

Here is my attempt

recode_factor(Data$i_ypsocweb, `1` = "yes", `2` = "no", missing = "missing")

The values aren't replaced and I'm seeing an error

NAs introduced by coercionUnreplaced values treated as NA as .x is not compatible. Please specify replacements exhaustively or supply .default

screenshot

What's even worse, when I axtually open the data set and find the value, it hasnt been changed. I still get numerical values

emilia.krb
  • 11
  • 3
  • See `?forcats::fct_explicit_na`. If you need further help, please provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Axeman Jan 27 '20 at 20:55

1 Answers1

0

There is no sample data, but one of these should get what you need. Sample data below where x is a numerical array and y is a factor.

library(dplyr)

set.seed(1)
x <- sample(c(1, 2, NA), size = 15, replace = TRUE)
y <- as.factor(x)

x %>% recode(`1` = "yes", `2` = "no", .missing = "missing")
[1] "yes"     "missing" "yes"     "no"      "yes"     "missing" "missing" "no"      "no"      "missing" "missing"
[12] "yes"     "yes"     "yes"     "no"

case_when(y == 1 ~ "yes", y == 2 ~ "no", is.na(y) ~ "missing")
[1] "yes"     "missing" "yes"     "no"      "yes"     "missing" "missing" "no"      "no"      "missing" "missing"
[12] "yes"     "yes"     "yes"     "no" 
manotheshark
  • 4,297
  • 17
  • 30