Suppose that I have the following data frame imported from SPSS:
y <- labelled_spss(c(1:4, 99), labels = c("Strongly Agree" = 1, "Somewhat Agree" = 2, "Somewhat Disagree" = 3, "Strongly Disagree" = 4, "DK/NA" = 99))
y
<labelled_spss<double>[5]>
[1] 1 2 3 4 99
Labels:
value label
1 Strongly Agree
2 Somewhat Agree
3 Somewhat Disagree
4 Strongly Disagree
99 DK/NA
And I use haven::as_factor()
to apply the labelled variables as factors:
y1 <- as_factor(y)
as.numeric(y1)
[1] 1 2 3 4 5
The function changes the underlying code value from DK/NA from 99 to 5. What adjustment can I make to avoid this underlying change to be done? That is, how could I use as_factor()
such that running as.numeric(y1)
would return:
[1] 1 2 3 4 99