Using haven::as_factor without changing the underlying integer codes

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))

[1]  1  2  3  4 99

 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)
[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

I think what you describe is simply at odds with how factors are understood in R. as factors are objects with integer backing with integer values 1:n where n are the number of levels required.
i.e. in your example 5.

That said, I believe you can continue to use the special haven type. when you want to access the numbers, use zap_labels


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.