Это одна из самых больших проблем с глобальным присваиванием <<-
внутри функции. У вашей функции return()
модель, не назначайте, и выполняйте назначение за пределами функции.
# function returns the result, doesn't assign it
upload <- function(filename) {
dat <- read.csv(file = filename)
lm(dat[,17]~dat[,6]+dat[,7])
}
# assignment happens outside the function (like almost every other R function)
# this way you can use whatever name you want
reg2 <- upload("hello.csv")
reg3 <- upload("world.csv")
# or use a for loop
reg <- list()
for (f in filenames) {
reg[[f]] <- upload(f)
}
# or use lapply for the same effect more concisely
reg <- lapply(filenames, upload)
names(reg) = filenames)
# You can now access individual list elements with [[
summary(reg[["hello.csv"]])
# Or extract all the model summary stats into a nice data frame
dplyr::bind_rows(lapply(reg, broom::glance))