With R, loop over data frames, and assign appropriate names to objects created in the loop

后端 未结 3 1739
一生所求
一生所求 2020-12-28 22:48

This is something which data analysts do all the time (especially when working with survey data which features missing responses.) It\'s common to first multiply impute a se

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-28 23:10

    Use a list to store the results of your regression models as well, e.g.

    foo <- function(n) return(transform(X <- as.data.frame(replicate(2, rnorm(n))), 
                                                           y = V1+V2+rnorm(n)))
    write.csv(foo(10), file="dat1.csv")
    write.csv(foo(10), file="dat2.csv")
    csvdat <- list.files(pattern="dat.*csv")
    lm.res <- list()
    for (i in seq(along=csvdat))
      lm.res[[i]] <- lm(y ~ ., data=read.csv(csvdat[i]))
    names(lm.res) <- csvdat
    

提交回复
热议问题