How to merge dataframes in R ?
Last Updated :
23 Jul, 2025
In this article, we will discuss how to perform inner, outer, left, or right joins in a given dataframe in R Programming Language.
Functions Used
merge() function is used to merge or join two tables. With appropriate values provided to specific parameters, we can create the desired join.
Syntax: merge(df1, df2, by.df1, by.df2, all.df1, all.df2, sort = TRUE)
Parameters:
df1: one dataframe
df2: another dataframe
by.df1, by.df2: The names of the columns that are common to both df1 and df2.
all, all.df1, all.df2: Logical values that actually specify the type of merging happens.
Inner join
An inner join also known as natural join, merges the two dataframes in one that contains the common elements of both. For this merge() function is simply given the values of two dataframes in consideration and on the basis of a common column a dataframe is generated.
Syntax:
merge(x = dataframe 1, y = data frame 2)
Example
R
# create data frame 1 with id ,
# name and address
df1=data.frame(id=c(7058,7059,7072,7075),
name=c("bobby","pinkey","harsha","deepika"),
address=c("kakumanu","hyd","tenali","chebrolu"))
# create data frame 2 with id ,
# marks
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063),
marks=c(90,78,98,67,89,90))
# display dataframe1
print(df1)
# display dataframe2
print(df2)
print("Inner join")
# inner join
print(merge(x = df1, y = df2))
Output:

Outer Join
Outer Join merges all the columns of both data frames into one for all elements. For this, the dataframes in consideration along with all parameter assigned value TRUE has to be passed to merge() function.
Syntax:
merge(x = data frame 1, y = data frame 2, all = TRUE)
Example:
R
# create data frame 1 with id , name and address
df1=data.frame(id=c(7058,7059,7072,7075),
name=c("bobby","pinkey","harsha","deepika"),
address=c("kakumanu","hyd","tenali","chebrolu"))
# create data frame 2 with id , marks
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063),
marks=c(90,78,98,67,89,90))
# display dataframe1
print(df1)
# display dataframe2
print(df2)
print("Inner join")
# outer join
print(merge(x = df1, y = df2,all=TRUE))
Output:
Note: It returns NA of unmatched columns
Left Join
It gives the data which are matching all the rows in the first data frame with the corresponding values on the second data frame. For this along with the dataframes in consideration, all parameter has to be passed TRUE after giving reference of the left table.
Syntax:
merge(x = data frame 1, y = data frame 2, all.x = TRUE)
Example:
R
# create data frame 1 with id , name and address
df1=data.frame(id=c(7058,7059,7072,7075),
name=c("bobby","pinkey","harsha","deepika"),
address=c("kakumanu","hyd","tenali","chebrolu"))
# create data frame 2 with id , marks
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063),
marks=c(90,78,98,67,89,90))
# display dataframe1
print(df1)
# display dataframe2
print(df2)
print("Left join")
# Left join
print(merge(x = df1, y = df2,all.x=TRUE))
Output:

Right Join
It gives the data which are matching all the rows in the second data frame with the corresponding values on the first data frame. For this merge() function should be provided with dataframes along with all parameters assigned TRUE. all parameters should have a reference to the right dataframe.
Syntax:
merge(x = data frame 1, y = data frame 2, all.y = TRUE)
Example:
R
# create data frame 1 with id , name and address
df1=data.frame(id=c(7058,7059,7072,7075),
name=c("bobby","pinkey","harsha","deepika"),
address=c("kakumanu","hyd","tenali","chebrolu"))
# create data frame 2 with id , marks
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063),
marks=c(90,78,98,67,89,90))
# display dataframe1
print(df1)
# display dataframe2
print(df2)
print("Right join")
# Right join
print(merge(x = df1, y = df2,all.y=TRUE))
Output:
Similar Reads
Merge two DataFrames in PySpark In this article, we will learn how to merge multiple data frames row-wise in PySpark. Outside chaining unions this is the only way to do it for DataFrames. The module used is pyspark : Spark (open-source Big-Data processing engine by Apache) is a cluster computing system. It is faster as compared to
4 min read
How to Delete DataFrames in R? In R, a DataFrame is a data structure which can be two-dimensional, that is it can be used to hold data in rows and columns. To create a DataFrame, you can use the data.frame() function. but after you're done with a DataFrame, you may wish to remove it so that memory can be released or your workspac
3 min read
How to Delete DataFrames in R? In R, a DataFrame is a data structure which can be two-dimensional, that is it can be used to hold data in rows and columns. To create a DataFrame, you can use the data.frame() function. but after you're done with a DataFrame, you may wish to remove it so that memory can be released or your workspac
3 min read
How to Merge Two Pandas DataFrames on Index Merging two pandas DataFrames on their index is necessary when working with datasets that share the same row identifiers but have different columns. The core idea is to align the rows of both DataFrames based on their indices, combining the respective columns into one unified DataFrame. To merge two
3 min read
How to create dataframe in R Dataframes are basic structures in R used to store and work with data in table format. They help us arrange data in rows and columns, like a spreadsheet or database table. We can use different ways to make dataframes while working with data in R.1. Creating and Combining Vectors Using data.frameWe m
2 min read
How to Handle merge Error in R R is a powerful programming language that is widely used for data analysis and statistical computation. The merge() function is an essential R utility for integrating datasets. However, combining datasets in R may occasionally result in errors, which can be unpleasant for users. Understanding how to
3 min read