Bohnston
Bohnston

Reputation: 69

Convert multiple .sav to .csv in R

I want to convert a bunch of .sav to .csv

I have a list of files like this:

c("Congo_MICS5_Datasets/Congo MICS 2014-15 SPSS Datasets/bh.sav", "Congo_MICS5_Datasets/Congo MICS 2014-15 SPSS Datasets/ch.sav", "Congo_MICS5_Datasets/Congo MICS 2014-15 SPSS Datasets/hh.sav")

How do i convert them and keep the file name?

Upvotes: 0

Views: 811

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78852

Assuming you're in the working directory that can access the relative paths and assuming you're using the foreign package and assuming you don't want row names (lots of assumptions since the question is sparse):

c(
  "Congo_MICS5_Datasets/Congo MICS 2014-15 SPSS Datasets/bh.sav", 
  "Congo_MICS5_Datasets/Congo MICS 2014-15 SPSS Datasets/ch.sav", 
  "Congo_MICS5_Datasets/Congo MICS 2014-15 SPSS Datasets/hh.sav"
) -> fils

for (fil in fils) {

  write.csv(
    x = foreign::read.spss(file = fil, to.data.frame = TRUE),
    file = sprintf("%s.csv", tools::file_path_sans_ext(fil)),
    row.names = FALSE
  )

}

Upvotes: 2

Related Questions