0

My issue is as follows - I have 100 datasets saved in the folder, the datasets are called dat1.csv - dat100.csv. I would like to perform regression on each of them. So far I have used following loop to do that:

cesta_data = gsub(pattern = "\\\\","/",readline())
\\srv332\nt332\data\R_Project\REGRESE\VSTUP

listova<-list.files(cesta_data)

for (i in 1:length(listova)){
  data<-read.csv2(paste(cesta_data,"/",listova[i],sep=""),header=TRUE)      
  formula1<-as.formula(anuita~KH)                                             
  regrese<-lm(formula = formula1, data = data)                             
  a<-regrese$coefficients                     
}

Each loop I upload the csv file, perform the regression and save the results (which is not included in the example of the code).

Is there a way how to create an "object" with R datasets that I can loop through ? Something like this:

cesta_data = gsub(pattern = "\\\\","/",readline())
\\srv332\nt332\data\R_Project\REGRESE\VSTUP

listova<-list.files(cesta_data)

for (i in 1:length(listova)){
  DATA[i]<-read.csv2(paste(cesta_data,"/",listova[i],sep=""),header=TRUE)      
  formula1<-as.formula(anuita~KH)                                             
  regrese<-lm(formula = formula1, data = DATA[i])                             
  a<-regrese$coefficients                     
}

Thank you a lot for any suggestions.

EDIT: I have created the following solution, which works fine:

listDat<-list.files(cesta_data)  #all the files in the folder

 my_data_list<-list()             
 for (i in seq_along(listDat)){
     my_data_list[[i]] = read.csv2(paste(cesta_data,"/",listDat[i],sep=""), header = TRUE) 
  }    

 for (i in seq_along(listDat)){
      regrese<-lm(formula = anuita~KH, data = my_data_list[[i]])
      a<-regrese$coefficients
  }
jiri jansa
  • 159
  • 1
  • 2
  • 15
  • 4
    Take a look at [How to import multiple .csv files at once?](https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once) – markus Mar 08 '19 at 08:13
  • 3
    Use `lapply` instead of your loops. You can use it to first create a list of data frames, and then a list of regression etc. – George Mar 08 '19 at 08:25
  • 1
    Looks like a case for purrr with map, particularly map_df – p0bs Mar 08 '19 at 08:55
  • thank you for all the suggestions, I have solved it through a list – jiri jansa Mar 08 '19 at 12:22

0 Answers0