## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", message = FALSE, warning = FALSE ) ## ----setup-------------------------------------------------------------------- library(mariposa) library(dplyr) data(survey_data) ## ----eval = FALSE------------------------------------------------------------- # # Read an SPSS file # data <- read_spss("survey_2024.sav") ## ----eval = FALSE------------------------------------------------------------- # # Tagged NAs are on by default # data <- read_spss("survey.sav", tag_na = TRUE) # # # Turn off if you just want regular NAs # data <- read_spss("survey.sav", tag_na = FALSE) ## ----eval = FALSE------------------------------------------------------------- # # See a breakdown of missing types # na_frequencies(data, q1, q2, q3) # # # Convert tagged NAs back to their original codes # data_with_codes <- untag_na(data, q1, q2) # # # Remove tags but keep NAs # data_clean <- strip_tags(data) ## ----eval = FALSE------------------------------------------------------------- # data <- read_stata("survey.dta") ## ----eval = FALSE------------------------------------------------------------- # # SAS data file # data <- read_sas("survey.sas7bdat") # # # With a separate catalog file for labels # data <- read_sas("survey.sas7bdat", catalog_file = "formats.sas7bcat") # # # SAS transport format # data <- read_xpt("survey.xpt") ## ----eval = FALSE------------------------------------------------------------- # data <- read_por("survey.por") ## ----eval = FALSE------------------------------------------------------------- # # Basic Excel import # data <- read_xlsx("survey.xlsx") # # # Excel file with label metadata (exported by write_xlsx) # data <- read_xlsx("survey.xlsx", label_sheet = "labels") ## ----eval = FALSE------------------------------------------------------------- # codebook(survey_data) ## ----------------------------------------------------------------------------- # Find variables related to "trust" find_var(survey_data, "trust") # Search by variable label find_var(survey_data, "satisfaction", search = "label") ## ----eval = FALSE------------------------------------------------------------- # # Basic export # write_spss(survey_data, "output.sav") # # # With compression options # write_spss(survey_data, "output.sav", compress = "zsav") # smaller file ## ----eval = FALSE------------------------------------------------------------- # write_stata(survey_data, "output.dta") ## ----eval = FALSE------------------------------------------------------------- # write_xpt(survey_data, "output.xpt") ## ----eval = FALSE------------------------------------------------------------- # # Export data only # write_xlsx(survey_data, "output.xlsx") # # # Export a codebook # cb <- codebook(survey_data) # write_xlsx(cb, "codebook.xlsx") # # # Export frequency tables # freq <- frequency(survey_data, education, employment) # write_xlsx(freq, "frequencies.xlsx") ## ----eval = FALSE------------------------------------------------------------- # # 1. Import from SPSS # original <- read_spss("survey.sav") # # # 2. Work with the data in R # processed <- original %>% # filter(age >= 18) %>% # mutate(age_group = rec(., age, rules = "18:29=1; 30:49=2; 50:99=3")) # # # 3. Export back to SPSS # write_spss(processed, "survey_processed.sav") # # # Variable labels, value labels, and missing value definitions # # are all preserved in the exported file.