setwd("/Users/benambridge/Desktop") # Change this to the name of the directory that the files are in # NB: This directory must contain ONLY the csv files you want to join together # If there are any other files of any type in this directory - inculding this R script file - # you will get an error message. NB: All files must have exactly the same layout file_names=dir() # This gets a list of all the files in the directory id=2:length(file_names) # This counts all the files in the directory data=read.csv(file_names[1],header=T) # This loads in the first file data$file=file_names[1] # This adds to the final sheet a column which contains the name of the original datafile for (i in id) { temp=read.csv(file_names[i], header=T) temp$file=file_names[i] data=rbind(data,temp) } # This loop loads in each file in turn, adds it to the master sheet, then puts the original filename in the final column write.csv(data, "RawData.csv") # This saves the final output file (in the same directory as the originals)