WeakLearner
WeakLearner

Reputation: 938

using loop variable in text string for loops in R

If i have multiple csv files stored as: m1.csv, m2.csv,.....,m50.csv and what I would like to do is load each csv into R, run the data in the i-th file and store it as a variable: m'i'. I am trying to use a for loop but i'm not sure if i can quite use them in such a way. For example:

for (i in 1:100){
     A<-as.matrix(read.csv("c:/Users/Desktop/m"i".csv))
     ...
     #some analysis on A
     ...
     m"i"<- #result of analysis on A
     }
     V<-cbind(m1,m2, .... ,m100)

Upvotes: 0

Views: 925

Answers (1)

Sunny Sunny
Sunny Sunny

Reputation: 3230

Try this

filenames = list.files(getwd())
filenames = filenames[grepl(".csv",file_names)]
files = lapply(filenames, read.csv)
files = do.call(rbind,files)

Upvotes: 1

Related Questions