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
Figure 2: Displaying data_2018
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.
Figure 3: Final Results — Appended Data Frame
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.
Done! I hope this article helps you learned a new thing today. Please share with others if you find it helpful.