使用R将Stata 16文件转换为Stata 12文件

使用R将Stata 16文件转换为Stata 12文件,r,stata,R,Stata,我正在使用RStudio(运行R4.0.1)和Stata12 for Windows,并且有大量包含Stata16.dta文件的文件夹(以及与此问题无关的其他类型的文件)。我想创建一个自动过程,将所有Stata 16.dta文件转换为Stata 12格式(保留所有标签),然后进行分析。 理想情况下,我希望保留原始文件夹和文件的名称,但将转换后的版本保存到新位置 到目前为止,我得到的是: setwd("C:/FilesLocation") #vector with name of files to

我正在使用RStudio(运行R4.0.1)和Stata12 for Windows,并且有大量包含Stata16.dta文件的文件夹(以及与此问题无关的其他类型的文件)。我想创建一个自动过程,将所有Stata 16.dta文件转换为Stata 12格式(保留所有标签),然后进行分析。 理想情况下,我希望保留原始文件夹和文件的名称,但将转换后的版本保存到新位置

到目前为止,我得到的是:

setwd("C:/FilesLocation")
#vector with name of files to be converted
all_files <- list.files(pattern="*.dta",full.names = TRUE)
for (i in all_files){

#Load file to be converted into STATA12 version
data <- read_dta("filename.dta",
                 encoding = NULL,
                 col_select = NULL,
                 skip = 0,
                 n_max = Inf,
                 .name_repair = "unique")

#Write as .dta
write_dta(data,"c:/directory/filename.dta", version = 12, label = attr(data, "label"))
}
setwd(“C:/FilesLocation”)
#带有要转换的文件名的向量

所有的文件您的代码只需要一些非常小的修改。我已经在下面的代码片段中指出了这些更改(以及解释它们的注释)

library(haven)

mypath <- "C:/FilesLocation"
all_files <- list.files(path = mypath, pattern = "*.dta", full.names = TRUE)

for (i in 1:length(all_files)){ 
  #(Above) iterations need the length of the vector to be specified

  #Load file to be converted into STATA12 version
  data <- read_dta(all_files[i], #You want to read the ith element in all_files
                   encoding = NULL,
                   col_select = NULL,
                   skip = 0,
                   n_max = Inf,
                   .name_repair = "unique")

  #Add a _v12 to the filename to 
  #specify that is is version 12 now
  new_fname <- paste0(unlist(strsplit(basename(all_files[i]), "\\."))[1], 
                     "_v12.", unlist(strsplit(basename(all_files[i]), "\\."))[2])

  #Write as .dta
  #with this new filename
  write_dta(data, path = paste0(mypath, "/", new_fname), 
            version = 12, label = attr(data, "label"))

}
然后,通过
lappy
for
循环调用函数,可以在任意数量的文件上运行该函数,如下例所示:

#----

#Example run
library(haven)

#Set your path here below
mypath <- paste0(getwd(), "/", "dta")

#Check to see if this directory exists
#if not, create it
if(!dir.exists(mypath)) dir.create(mypath)
list.files(mypath)
# character(0)

#----
#Downloading some valid example files
myurl <- c("http://www.principlesofeconometrics.com/stata/airline.dta", 
           "http://www.principlesofeconometrics.com/stata/cola.dta")
lapply(myurl, function(x){ download.file (url = x, destfile = paste0(mypath, "/", basename(x)))})

#Also creating a negative test case
file.create(paste0(mypath, "/", "anegcase.dta"))

list.files(mypath)
# [1] "airline.dta"  "anegcase.dta" "cola.dta" 
#----


#Getting list of files in the directory
all_files <- list.files(path = mypath, pattern = "*.dta")


#Converting files using dtavconv via lapply
res <- lapply(all_files, dtavconv, mypath = mypath)
# 
# Successfully converted airline.dta to airline_v12.dta
# 
# 
# Error in df_parse_dta_file(spec, encoding, cols_skip, n_max, skip, 
# name_repair = .name_repair): Failed to parse /my/path/
# /dta/anegcase.dta: Unable to read from file.
# 
# 
# 
# Successfully converted cola.dta to cola_v12.dta
# 

list.files(mypath)
# [1] "airline_v12.dta" "airline.dta"     "anegcase.dta"    "cola_v12.dta"    
# "cola.dta" 

#Example for converting to version 14
res <- lapply(all_files, dtavconv, mypath = mypath, myver = 14)
# 
# Successfully converted airline.dta to airline_v14.dta
# 
# 
# Error in df_parse_dta_file(spec, encoding, cols_skip, n_max, skip, 
# name_repair = .name_repair): Failed to parse /my/path
# /dta/anegcase.dta: Unable to read from file.
# 
# 
# 
# Successfully converted cola.dta to cola_v14.dta
# 

list.files(mypath)
# [1] "airline_v12.dta" "airline_v14.dta" "airline.dta"     "anegcase.dta"    
# "cola_v12.dta"    "cola_v14.dta"    "cola.dta" 

#----
#----
#示例运行
图书馆(避风港)
#在下面设置您的路径

mypath感谢您的及时回复。我已经实现了代码,它工作得很好。伟大的贡献。
#----

#Example run
library(haven)

#Set your path here below
mypath <- paste0(getwd(), "/", "dta")

#Check to see if this directory exists
#if not, create it
if(!dir.exists(mypath)) dir.create(mypath)
list.files(mypath)
# character(0)

#----
#Downloading some valid example files
myurl <- c("http://www.principlesofeconometrics.com/stata/airline.dta", 
           "http://www.principlesofeconometrics.com/stata/cola.dta")
lapply(myurl, function(x){ download.file (url = x, destfile = paste0(mypath, "/", basename(x)))})

#Also creating a negative test case
file.create(paste0(mypath, "/", "anegcase.dta"))

list.files(mypath)
# [1] "airline.dta"  "anegcase.dta" "cola.dta" 
#----


#Getting list of files in the directory
all_files <- list.files(path = mypath, pattern = "*.dta")


#Converting files using dtavconv via lapply
res <- lapply(all_files, dtavconv, mypath = mypath)
# 
# Successfully converted airline.dta to airline_v12.dta
# 
# 
# Error in df_parse_dta_file(spec, encoding, cols_skip, n_max, skip, 
# name_repair = .name_repair): Failed to parse /my/path/
# /dta/anegcase.dta: Unable to read from file.
# 
# 
# 
# Successfully converted cola.dta to cola_v12.dta
# 

list.files(mypath)
# [1] "airline_v12.dta" "airline.dta"     "anegcase.dta"    "cola_v12.dta"    
# "cola.dta" 

#Example for converting to version 14
res <- lapply(all_files, dtavconv, mypath = mypath, myver = 14)
# 
# Successfully converted airline.dta to airline_v14.dta
# 
# 
# Error in df_parse_dta_file(spec, encoding, cols_skip, n_max, skip, 
# name_repair = .name_repair): Failed to parse /my/path
# /dta/anegcase.dta: Unable to read from file.
# 
# 
# 
# Successfully converted cola.dta to cola_v14.dta
# 

list.files(mypath)
# [1] "airline_v12.dta" "airline_v14.dta" "airline.dta"     "anegcase.dta"    
# "cola_v12.dta"    "cola_v14.dta"    "cola.dta" 

#----