I have 50 Stata-formatted data sets that I would like to read into R and save as RData
sets. Currently, my code looks like this:
# Package to read Stata data sets into R
library(haven)
# Data set 1: Read Stata data into R
dataset1 <- read_dta("C:/dataset1.dta")
# Save as RData
save(dataset1, file = "C:/RData/dataset1.Rdata")
# Data set 2: Read Stata data into R
dataset2 <- read_dta("C:/dataset2.dta")
# Save as RData
save(dataset2, file = "C:/RData/dataset2.Rdata")
This is clunky and takes up many lines of code. I would like to create a function or a loop that will go thorough this efficiently and is easier to understand and debug.
This code gets me almost there (thanks @canyon), except that when I load the data files, they all have the name "import_data" name. The files themselves are named correctly (i.e., dataset1.Rdata, dataset2.Rdata), but when loaded into R, the environment name is "import_data". This is problematic as I can't have more than 1 of the files open in the same environment as it will override the existing one (e.g., dataset2.Rdata will override dataset1.Rdata). Is there a way to save the files with a name that matches the file =
option in save
?
library(haven)
library(stringr)
your_function <- function(x) {
import_path <- str_c("C:/dataset", (x), ".dta")
import_data <- read_dta(import_path)
save_path <- str_c("C:/RData/dataset", (x), ".Rdata")
save(import_data, file = save_path)
}
lapply(1:50, your_function)
I looked at linked posts that seemingly address this issue, but none of them solve this specific issue.