![](/img/trans.png)
[英]In R how to loop through csv files and safe outputs of linear regression in new dataframe?
[英]Linear regression in R, loop through csv files
如果另一个问题中已经有可用的解决方案,我深表歉意。 我有 500 多个 csv 文件,所有文件都具有顺序名称“name1、name2 等”。 我需要对每个文件运行相同的简单线性回归并保存系数输出。 每个文件的列名也相同,并且只有一个单一的 x 和 y 变量。
我只知道怎么用
lm(tablename$columnY~tablename$columnX)
在单个文件上。 我不确定如何设置一个循环来遍历每个文件。
任何帮助表示赞赏
这是我使用函数运行它的方法,(您也可以使用 for 循环)。 原则是命名所有有问题的文件并一个一个地浏览它们,边走边保存每个模型的结果。
#put all your files in one folder and point to the folder path
path <- "C:/Users/xxx/Desktop"
#list all the files, with directory attached
lst <- list.files(path, full.names = T)
#make a function or loop (i like functions to get structured output)
fun <- function(i){
#read each csv one at a time
dat <- read.csv(lst[i])
#make the model
mod <- lm(dat$columnY~dat$columnX)
#extract the information from the model (press view on any model and chose the desired values and hjust copy that code)
intcpt <- mod[["coefficients"]][["(Intercept)"]]
y <- mod[["coefficients"]][["columnX"]]
#set into dataframe, with the name of the file
out <- data.frame(lst[i], intcpt, y)
}
temp <- lapply(1:length(lst), fun) #run the model (will take the last thing stated in the fuction and make a list elemnt for each "loop")
results <- do.call("rbind",temp) #from list to dataframe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.