How can I convert to dataframe, but drawing in data labels rather than content?

39 Views Asked by At

I've switched recently from using CSV or XLSX outputs from qualtrics surveys to using SAV format data which I can analyse in SPSS and then pass on to R. One key challenge for R is the ways that survey data can appear as numbered responses ("1", "2", "3", etc.) but those numbers also correspond to specific items in your codebook ("pizza", "hot dogs", "veggie sausages", etc.). The haven() package, now absorbed into tidyverse() offers a really useful way to draw in a complete data set, using (I believe) labels to include the names of values, and rendering data as a tibble:

data <- read_sav(here("gits", "survey", "data", "survey.sav")) %>% select(Q0:Q68)

For some tools, I need to reduce data back down to a data-frame and zap all the labels, and that is pretty straight forward to achieve:

as.data.frame(as_factor(haven_spss_data$Q6))

However, I'd also like to have a data-frame consisting of the response text for a given item, e.g. the labels. How would I draw in that data as ordered factors (like above) but based on labels instead of content?

1

There are 1 best solutions below

0
eli-k On

This should do it:

library(foreign)
df <- read.spss("yourfile.sav",to.data.frame=TRUE)

The foreign::read.spss function by default uses spss value labels instead of values where they are available.