Last active
September 29, 2021 09:50
-
-
Save jflanaga/e8f4a4698a93e8f4488cfc8cd663d3d1 to your computer and use it in GitHub Desktop.
Read in multiple spss files and read out multiple csv files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Modified from https://martinctc.github.io/blog/vignette-write-and-read-multiple-excel-files-with-purrr/ | |
# Note: this will return numeric codes rather than value labels. Use `as_factor()` to get the latter | |
library(tidyverse) | |
# function for writing the csv files | |
output_csv <- function(data, names){ | |
# output directory | |
folder_path <- "data/" | |
# too lazy to use regular expressions to extract name of the file | |
write_csv(data, paste0(folder_path, str_sub(names, start=15, end=-5), ".csv")) | |
} | |
# Get list of files in directory containing spss files | |
data_dir <- fs::dir_ls("SPSS_data_Sets") | |
# read in spss files | |
list_of_dfs <- data_dir %>% | |
map(read_sav) | |
# create a list of dataframes and their names | |
list(data = list_of_dfs, | |
names = names(list_of_dfs)) %>% | |
# Isn't purr great? | |
purrr::pmap(output_csv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment