R Merge CSV Files: How To Merge CSV Files In R

merge csv files in r

In this tutorial, we will get knowledge about R merge CSV files. When working with CSV files in R, you usually have a need to merge those CSV files. We will provide you with two popular solutions: using rbind() and merge(). These are built-in functions so you don’t need to install external packages.

Merge csv files in R

Using rbind()

Rbind() stands for row binding. It will join multiple rows into a table.

Syntax:

rbind(x,y,..z)

Parameters:

  • x: This is the first data frame or objects to be merged.
  • y: This is the second data frame or objects to be merged.
  • z: This is the last data frame or objects to be merged.

Assume you have three CSV files that contain the same data structure as follows: 

Category, Views
R, 2555
JavaScript, 754
Category, Views
Python, 355
SQL, 1823
Category, Views
C++, 9999
Go, -1

We will use the read.csv() function to read and store data in those files to merge them later.

# Read the first CSV file and store it in a data frame
data1 <- read.csv(file='learnshareit1.csv',sep=',',header=T)
 
# Read the second CSV file and store it in a data frame
data2 <- read.csv(file='learnshareit2.csv',sep=',',header=T)
 
# Read the last CSV file and store it in a data frame
data3 <- read.csv(file='learnshareit3.csv',sep=',',header=T)
 
# Merge all CSV files read
learnshareit <- rbind(data1, data2, data3)
 
# Display the result
learnshareit

Output

> learnshareit
    Category Views
1          R  2555
2 JavaScript   754
3     Python   355
4        SQL  1823
5        C++  9999
6         Go    -1

As can be seen, after we merge CSV files in R, we receive a data frame with one header row, and the data rows are all the rows that are represented in each CSV file. This is just an example of merging three files; however, if you have more files, you can apply the same read method and then rbind() function with more arguments as much as you want.

Using merge()

To perform R merge CSV files, you can use merge() function in R (See the syntax here). To make it simple, we will reuse the first two CSV files we have declared in the previous solution and just apply the method on two data tables stored only:

# Merge two CSV files read
learnshareit <- merge(data1, data2, all=TRUE)
 
# Display the result
learnshareit

Output

> learnshareit
    Category Views
1 JavaScript   754
2          R  2555
3     Python   355
4        SQL  1823

Can you realize the differences between the two results in each solution? This merge function can merge two CSV files instead of many files like the rbind() function. Therefore, if you want to merge more than two csv files, you should reuse more merge() functions as follows:

# Merge first and second csv files 
learnshareit1 <- merge(data1, data2, all=TRUE)

# Merge the merged table and last csv files 
learnshareit <- merge(learnshareit1, data3, all=TRUE)

# Display the result
learnshareit

Output:

> learnshareit
    Category Views
1 JavaScript   754
2          R  2555
3     Python   355
4        SQL  1823
5        C++  9999
6         Go    -1

However, you should remember to pass an argument all = TRUE to this function if you want to merge the data rows in these files.

Summary

We have learned how to merge CSV files in R using two different approaches. Depending on how many files you want to merge, you can consider using any function. Generally, the first solution is proved more efficient when solving this problem. Let’s try it!

Maybe you are interested:

Posted in R

Leave a Reply

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