如何批处理将所有 .sav 转换为 R 文件夹中的平面文件?

How to batch process converting all .sav to flat file that are in a folder in R ?

下面是我将 .sav 转换为运行良好的平面文件格式的代码,当我有超过 50 个文件时,我的问题就会出现。关于如何循环处理文件夹中所有可用文件的任何建议?

  #load library foreign to read spss 
  library(foreign) 

  #set working directory first 
  setwd("M:\Files\Linear Reg") 

  #read .sav file 
  data <-read.spss('Computed_Copy.sav', to.data.frame=TRUE,use.value.labels=FALSE) 
  write.csv(data, "Computed_Copy.csv")

首先列出文件夹中以 .sav 结尾的所有文件

files <- list.files(path = 'your/path/tofolder', pattern = '.sav')
for(f in files){ # iterate over them 
  data <-read.spss(f, to.data.frame=TRUE,use.value.labels=FALSE) 
  write.csv(data, paste0(strsplit(f, split = '.', fixed =  T)[[1]][1], '.csv')) 
# the stringsplit removes the wrong ending and the paste adds .csv
}