0

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)
3
  • You'll need to use paste0 to assemble paths and names, and probably assign instead of <-, as your name will be quoted, but yes, the approach is pretty common. You won't want to do analysis in the loop, though; save that for later. You may also want to save them into a list—instead of the global environment—to make looping over them later easier. It also simplifies naming. Commented Mar 16, 2016 at 6:31
  • 1
    @alistaire While I can agree that OP's approach is pretty common, I don't think it can be considered "correct" and it shouldn't be encouraged. The use of assign is almost never a good idea. A simple lapply would solve OP's task in a cleaner way. Commented Mar 16, 2016 at 6:36
  • 1
    That is why God created lists. Commented Mar 16, 2016 at 7:18

1 Answer 1

1

Try this

filenames = list.files(getwd())
filenames = filenames[grepl(".csv",file_names)]
files = lapply(filenames, read.csv)
files = do.call(rbind,files)
Sign up to request clarification or add additional context in comments.

7 Comments

The idea is good, but there are some inaccuracies. 1) No need to call list.files, since in the OP the file names are specified: filenames<-paste0("c:/Users/Desktop/m",1:100,".csv") should be the way. 2) The analysis part is missing: OP should define a function (let's call it makeAnalysis) that does the task. 3) At the end, OP wants to cbind rather than rbind. A simple line should be do.call(cbind,lapply(filenames, function(x) makeAnalysis(read.csv(x)))).
@nicola I've written up a function as you recommended, im just wodnering if i want to add the parameter row.names=1 to read.csv where should i do that? it seems to give me an error if i put it in the do.call
@nicola further, as my function involves matrix calculations, i get the non-conformable arguments error, not quite sure how to deal with this
@dimebucker91 Arguments of read.csv should go into the read.csv call. For instance, in my line you should replace read.csv(x) with read.csv(x,row.names=1). For the second issue, I suspect that it's a bug of your function and cannot tell for sure to what it is due. It seems that you are trying to make a matrix product between matrices with wrong numbers of row or cols.
@dimebucker91 Please, check what you wrote. Once it's row.names and the other rownames. The error message was clear, you should have solved it by yourself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.