Home » Uncategorized

How to combine all csv files from the same folder into one data frame automatically with R?

Have you ever experience a situation where you want to import and combine hundred of datasets? If you do this manually, then it will take too much time. On the other hand, we can use a simple R programming to solve this problem in easily and quickly. Therefore, I would like to share two methods of combining all small files in a directory into a single dataset automatically.

Consider, you have multiple small csv files that you put in the same folder as shown in Figure 1. Each csv file contains specific information for each year. You want to append all the files and have a data frame for all year. Figure 2 displays how the small dataset look like by displaying data_2018

Figure 1: Directory for All Files

1d97-CYclftyYSk8K0Zk0UQ

Figure 2: Displaying data_2018

17ZzHRmsrfSOocqBzs0Irxw

First Method

We are applying for-loop to importing each file and then append all small datasets. Code below shows the full step on how to do this. Step 1, we set the directory of small datasets. Step 2, we read each file by looping it with list.files() command. Step 3, we append the data using rbind command. You can see the final result of the combined data in Figure 3.

4201181980


Figure 3: Final Results — Appended Data Frame

1Dv91a-b6uf0It9afzI0IDQ

Second Method

We are using plyr package to read all the files and merge them right away. You can view the full code below . It is a very quick and simple way. Also, it gives the same result as the looping method above.
4201197211Done! I hope this article helps you learned a new thing today. Please share with others if you find it helpful.

Cheers,

Michaelino Mervisiano