Import multiple CSV files in R and load them all together in a single data frame

Import multiple CSV files in R and load them all together in a single data frame

List of all the filenames

One approach I found really straight forward is to create a list of all your filenames.
You can also create a pattern to fetch your directory and returns all the matching files.
In my example I need to read all the files starting with “FR”.

setwd("H:/R Projetcs/Accidents")
fileNames<-Sys.glob("csv/FR*.csv")
zonnesFiles<- lapply(fileNames, read.csv)

The function lapply (equivalent of a loop) reads every single file presents in my list fileNames and store them into my variable zonnesFiles.
The variable zonnesFiles is a list of data frames, I have to read 15 files so there’s 15 different dataframes in the list.

Merge all the files into a single data frame

Once we have our list of dataframe we want to merge them in one single dataframe.
As my files don’t have any headers I first need to make sure they all have the same column names, to do so I loop through my list of zonnesFiles and rename them.

I then create a function “merge.all”, my function just call the base r “merge” function but I like to create my own so I don’t have to bother with parameter every time I need to call the function.
Finally we just need to call our function for every single df in the zonnesFIles list.
I use the Reduce function to successively merge each dataframe of my list. The Reduce function takes a binary function and a vector/list and successively applies the function to the list elements.

And here is the code:

#Rename column names of each df
for(i in 1:length(zonnesFiles)){
 colnames(zonnesFiles[[i]])<-c("Longitude","Latitude","Type")
}

#Create a function to merge my df
merge.all<- function(x, y) {
 merge(x, y, all=TRUE, by=listCols)
}

#Lits of columns to merge on
listCols<-c("Longitude","Latitude","Type")
#call the merge function
zonnes<- Reduce(merge.all, zonnesFiles)

Leave a Reply

Your email address will not be published. Required fields are marked *