首页 > 解决方案 > Reading Multiple CSV files and perform a logistic regression for all those files separately

问题描述

I am trying to perform a logistic regression analysis on multiple CSV files. All the files have the same column's name ( DateTime, HvacMode, Event, Schedule,...). How can I load all the CSV files in the folder and perform a logistic using one Rstudio code for all the CSV files and I will be able to refer to each analysis's result in the future. My goal is to find a pattern in different CSV samples.

for a single file I use

glm(Event ~ T_ctrl + T_out+RH_out+ T_stp_cool+T_stp_heat+Humi, data=logistic, 
                        family=binomial(link="logit"))

and I need to do this for all the csv files in WD.

Thank you

标签: r

解决方案


您可以使用以下内容读取所有 csv 文件:

temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.csv)

接下来,您可以使用以下命令一次运行回归模型:

regression = function(df) {
  glm(Event ~ T_ctrl + T_out + RH_out + T_stp_cool + T_stp_heat+Humi, data = df, 
                        family = binomial(link = "logit"))
}

models = map(myfiles, regression)

推荐阅读