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
}