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