Reputation: 21
I'm working on analyzing the data from a survey. I have imported data from a SPSS file using haven. Now all the variables have the haven_labelled class. I'm very confused with the data types now. The survey is very long and in the end it should have character variables, factor variables and numeric variables. How can I switch the classes of the variables? Like first, I have tried to use the unclass() function, but that doesn't seem to work. Then I converted all variables into factors, but most of them need to be numeric, and to convert them one by one would be so much work. Also, it would be great to keep the labels. Like right now there is the variable name obviously, like 'gender', but each variable also has a label like 'What gender do you identify as?'. Whenever I convert that, the label goes missing. Also, some numeric data, like a scale from 1 - 10 also have labels, like 1 = 'strongly disagree' and 10 'strongly agree'.
Help would be much appreciated!
Upvotes: 0
Views: 72
Reputation: 21757
If you did something like this, it should turn your haven_labelled
variables into factors.
library(haven)
library(dplyr)
data %>% mutate(across(where(is.labelled), as_factor))
Upvotes: 1