[英]Linear regression in R, loop through csv files
I apologize if there is already a solution available in another question.如果另一个问题中已经有可用的解决方案,我深表歉意。 I have 500+ csv files all with sequential names "name1, name2, etc."
我有 500 多个 csv 文件,所有文件都具有顺序名称“name1、name2 等”。 I need to run the same, simple linear regression on each file and save the coefficient outputs.
我需要对每个文件运行相同的简单线性回归并保存系数输出。 The column names are also the same for each file and there is only a singular x & y variable.
每个文件的列名也相同,并且只有一个单一的 x 和 y 变量。
I only know how to use我只知道怎么用
lm(tablename$columnY~tablename$columnX)
on individual files.在单个文件上。 I'm not sure how to set up a loop to run through each file.
我不确定如何设置一个循环来遍历每个文件。
Any help is appreciated任何帮助表示赞赏
Here is how I would run it using a function, (you can also use a for loop).这是我使用函数运行它的方法,(您也可以使用 for 循环)。 the principle is to name all the files in question and go trough them one by one, saving the results of each model as we go.
原则是命名所有有问题的文件并一个一个地浏览它们,边走边保存每个模型的结果。
#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.