-3

I managed to create a loop that opens the csv files that I have in a folder and adds the file name in the first column. However, I don’t know how to put together all the tables created so that the final output after the loop is one data.frame with all the csv together (with the file name as the first column). I tried with cbind but it doesn’t work. Any help will be much appreciated. This is my attempt:

csvfilenames <- list.files("/Users/carlos/Desktop/TestCSVFilesToMerge/",
                   pattern="*.csv", all.files=FALSE, full.names=FALSE) #creates a list with the file names
csvfilenames

for(i in 1:length(csvfilenames)) 

{
a=csvfilenames[i]
temp1<-read.csv(file=paste("/Users/carlos/Desktop/TestCSVFilesToMerge/",a,sep=""), sep=";", header=T)
temp2<-cbind("FileName"=a,temp1[,1:ncol(temp1)]) #add a column called FileName in position 1
temp[a]=temp2 
}
output=rbind(temp[[a]])
output
Carlos
  • 47
  • 6
  • 1
    I would use `purrr::map_df` with the .id argument will work on a named vector of files – Richard Telford Apr 16 '18 at 09:35
  • 1
    What's your error? Please, post it in the question. – Terru_theTerror Apr 16 '18 at 09:54
  • 1
    Possible duplicate of [Reading multiple (excel) files into R - Best practice](https://stackoverflow.com/questions/32888757/reading-multiple-excel-files-into-r-best-practice) – h3rm4n Apr 16 '18 at 10:12
  • I don’t get any errors but “output” gives only the table of the last csv file. Therefore, it looks like the loop is not saving and pasting “temp2” to the previous one. – Carlos Apr 16 '18 at 10:35
  • I know how to bind them together out of the loop the problem it to do it in the loop. I have many files (all with the same structure). – Carlos Apr 16 '18 at 10:38
  • [This question is being discussed on meta.](https://meta.stackoverflow.com/questions/366075/how-to-deal-with-answer-coming-from-the-original-asker-providing-my-solution) – Script47 Apr 18 '18 at 14:08

1 Answers1

18

Try this way:

1 - define temp outside the for-loop

temp<-NULL

2 - Assign temp2 inside the for-loop like this:

temp[[a]]=temp2 

3 - Bind all the data.frames after the for-loop:

library(dplyr)
output<-bind_rows(temp)

Full code reviewed:

csvfilenames <- list.files("/Users/carlos/Desktop/TestCSVFilesToMerge/",
                   pattern="*.csv", all.files=FALSE, full.names=FALSE) #creates a list with the file names
csvfilenames

temp<-NULL

for(i in 1:length(csvfilenames)) 

{
a=csvfilenames[i]
temp1<-read.csv(file=paste("/Users/carlos/Desktop/TestCSVFilesToMerge/",a,sep=""),sep=";", header=T)
temp2<-cbind("FileName"=a,temp1[,1:ncol(temp1)]) #add a column called FileName in position 1
temp[[a]]=temp2 
}

library(dplyr)
output<-bind_rows(temp)
Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39
  • 2
    in output you are putting only the last csv, using [[a]], answer changed according to this point – Terru_theTerror Apr 16 '18 at 10:40
  • 2
    I cannot, in good faith, upvote this answer. A good answer should not just regurgitate the code in the question with unexplained corrections. It should explain _why_ the corrections fix the problems in the original code, and link to relevant documentation, if necessary. – Patrick Roberts Apr 18 '18 at 17:17