I am trying to merge several data.frames into one data.frame. Since I have a whole list of files I am trying to do it with a loop structure.
So far the loop approach works fine. However, it looks pretty inefficient and I am wondering if there is a faster and easier approach.
Here is the scenario:
I have a directory with several .csv files. Each file contains the same identifier which can be used as the merger variable. Since the files are rather large in size I thought to read each file one at a time into R instead of reading all files at once.
So I get all the files of the directory with list.files and read in the first two files. Afterwards I use merge to get one data.frame.
FileNames <- list.files(path=".../tempDataFolder/")
FirstFile <- read.csv(file=paste(".../tempDataFolder/", FileNames[1], sep=""),
             header=T, na.strings="NULL")
SecondFile <- read.csv(file=paste(".../tempDataFolder/", FileNames[2], sep=""),
              header=T, na.strings="NULL")
dataMerge <- merge(FirstFile, SecondFile, by=c("COUNTRYNAME", "COUNTRYCODE", "Year"),
             all=T)
Now I use a for loop to get all the remaining .csv files and merge them into the already existing data.frame:
for(i in 3:length(FileNames)){ 
ReadInMerge <- read.csv(file=paste(".../tempDataFolder/", FileNames[i], sep=""),
               header=T, na.strings="NULL")
dataMerge <- merge(dataMerge, ReadInMerge, by=c("COUNTRYNAME", "COUNTRYCODE", "Year"),
             all=T)
}
Even though it works just fine I was wondering if there is a more elegant way to get the job done?
 
     
     
    