Огромное спасибо Парфе за то, что он указал мне правильное направление, особенно с paste0s.Я действительно боролся с тем, что split (), кажется, переименовывает столбцы каждого DF в списке, который нарушает код (заставляет его возвращать NULL).
Это еще одно решение, которое более точно соответствует тому, что мне нужнокод, чтобы сделать в дикой природе (т.е. не на небольшой набор данных, как lalonde).Надеюсь, это пригодится кому-то в будущем.
## packages
library(tidyverse)
library(MatchIt)
##data
data("lalonde")
## randomize the data because lalonde is sorted by treated so the mathcing will fail for some subsets
lalonde2 <- lalonde[sample(nrow(lalonde)),]
##set the size of each subset
n <- 30
nr <- nrow(lalonde2)
### make the subsets
splitter <- split(lalonde2, rep(1:ceiling(nr/n), each=n, length.out=nr))
## write them to file (with replaced names because split() changed them)
for(i in 1:length(splitter)){
names(splitter[[i]]) <- c("treat", "age", "educ", "black", "hispan", "married", "nodegree", "re74", "re75", "re78")
write.csv(splitter[[i]], file = paste0("data_", i, ".csv"))
}
## remove the big one
rm(splitter)
## for loop that runs through each of the saved files from earlier, runs a matching model and matches the data and writes it to a file all in one
require(stringr)
for (i in 1:ceiling(nr/n)){
file<- read.csv(str_c("data_",i,".csv"))
write.csv(match.data(matchit(treat ~ age + educ, method = "nearest", data = file, ratio = 1)), file = paste0("matched_data_", i, ".csv"))
### remove the data after each iteration
rm(file)
}