View source: R/clean_spss_data.R
clean_spss_data | R Documentation |
haven
This function cleans SPSS .sav
data imported using haven
, converting
labelled variables into factors and cleaning variable names. It also
creates a data dictionary and a list of factor variables with their levels
and labels.
clean_spss_data(data, method = c("manual", "forcats"))
data |
A data frame or tibble imported with |
method |
A string specifying how to convert labelled variables to factors.
Options are |
A list with the following components:
A cleaned data frame with labelled variables converted to factors.
A tibble showing variable names and labels.
A named list of variables with labels, showing levels and names.
## Not run:
# Raw_data can be downloaded from here and saved to disc
# https://www.cambridge.org/us/academic/subjects/psychology/psychology-research-methods-and-statistics/statistics-using-ibm-spss-integrative-approach-3rd-edition?format=PB&isbn=9781107461222
raw_data <- haven::read_spss(file = "/Users/latour/Dropbox/code/R/qualtrics/Wages.sav")
cleaned <- clean_spss_data(raw_data, method = "forcats")
str(cleaned$data)
# Apply labels to the data
labels_list <- stats::setNames(object = as.list(cleaned$dictionary$lbl),
nm = cleaned$dictionary$var)
labelled::var_label(cleaned$data) <- labels_list
# See that now there are attributes where the labels are stored.
str(cleaned$data)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.