1

I have 1000 files .dta in a folder :

C:/Folder/data1.dta
C:/Folder/data2.dta
C:/Folder/data" ".dta
C:/Folder/data1000.dta

I nead to apply a program to impute my 1000 data, and save this imputed data in a new folder.

C:/Folder2/data_new1.dta
C:/Folder2/data_new2.dta
C:/Folder2/data_new" ".dta
C:/Folder2/data_new1000.dta

I have built a complex programm with LEA package, to realize a completion with non-negative matrix factorization. It is work like that :

data1 <- read.dta("C:/Folder/data1.dta")

***********My program******

write.dta (data1, "C:/Folder/data_new1.dta")

I search a method to loop this for my 1000 datasets. With Stata, i can do like that :

forvalues i=1/1000 {

data`i' <- read.dta("C:/Folder/data`i'.dta")

***********My program******

write.dta (data`i', "C:/Folder/data_new`i'.dta")

1 Answer 1

1

Use a list:

f <- dir(pattern = "dta")
dat <- lapply(f, read.dta)
# do stuff to every data.frame
for (i in seq_along(dat)) {
  write.dta(dat[[i]], paste0("data_new", i, ".dta"))
}
Sign up to request clarification or add additional context in comments.

1 Comment

The "do stuff to every data frame" and the write.dta could both be included inside lapply, with no need to actually save the list to an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.