Open In App

Merge Two Data Frames by common Columns in R Programming – merge() Function

Last Updated : 12 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

merge() function in R Language is used to merge two data frames by common columns.

Syntax: merge(arg1, arg2, by.x, by.y)

Parameters:
arg1 and arg2: Data frames to be merged
by.x: Common argument of first data frame
by.y: Common argument of second data frame

Example 1:




# R program to merge two data frames
  
# Creating data frames
df1 <- data.frame(row1 = c("a", "b", "c"), row2 = c("d", "e", "f"))
df2 <- data.frame(col1 = c("a", "b", "c"), col2 = c("Hello", "Geeks", "gfg"))
  
# Calling merge() function
df <- merge(df1, df2, by.x ="row1", by.y ="col1")
print(df)


Output:

  row1 row2  col2
1    a    d Hello
2    b    e Geeks
3    c    f   gfg

Example 2:




# R program to merge two data frames
  
# Creating data frames
df1 <- data.frame(row1 = c("d", "e", "f"), row2 = c(1, 2, 3))
df2 <- data.frame(col1 = c(1, 2, 3), col2 = c("Hello", "Geeks", "gfg"))
  
# Calling merge() function
df <- merge(df1, df2, by.x ="row2", by.y ="col1")
print(df)


Output:

  row2 row1  col2
1    1    d Hello
2    2    e Geeks
3    3    f   gfg


Similar Reads

Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function
cbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by columns. Syntax: cbind(x1, x2, ..., deparse.level = 1) Parameters: x1, x2: vector, matrix, data frames deparse.level: This value determines how the column names generated. The default value of deparse.level is 1. Example 1: # R program to illustrate # cbind
2 min read
Comparing values of data frames in R Programming - all_equal() Function
all_equal() function in R Language is used to compare the values between dataframes. Syntax: all_equal(target, current, check.attributes, check.names) Parameters: target: object to comparecurrent: object to be compared withcheck.attributes: If attributes of both be comparedcheck.names: If names be comparedR - compare values of data frames Example 1
2 min read
Combine Vectors, Matrix or Data Frames by Rows in R Language - rbind() Function
In this article, we will discuss how we Combine Vectors, Matrices, or Data Frames by Rows in R Programming Language using rbind function. [caption width="800"]rbind() Function in R[/caption]What is rbind function?The rbind function combines or concatenates data frames or matrices by rows. the rbind stands for row binds it shows that it appends rows
3 min read
Manipulate R Data Frames Using SQL
Manipulating data frames in R Programming using SQL can be easily done using the sqldf package. This package in R provides a mechanism that allows data frame manipulation with SQL and also helps to connect with a limited number of databases. The sqldf package in R is basically used for executing the SQL commands or statements on data frames. One ca
8 min read
R - Data Frames
R Programming Language is an open-source programming language that is widely used as a statistical software and data analysis tool. Data Frames in R Language are generic data objects of R that are used to store tabular data.  Data frames can also be interpreted as matrices where each column of a matrix can be of different data types. R DataFrame is
10 min read
Indexing and Slicing Data Frames in R
In this article let's discuss indexing and slicing the Data Frames or how to access elements of a data frame in R Programming Language. What is Indexing or accessing?The process of accessing particular data components or subsets within a vector, matrix, or data frame is called indexing. It enables us to pick, remove, or change particular values or
4 min read
Get and Set Row Names for Data Frames
In this article, we will explore various methods to Get and Set row names for Data Frames in R Programming Language. What is Data Frames?Dataframes are the 2- dimensional data structure which organizes the data into rows and columns. These data frames are commonly used for manipulation, analysis, and visualization of data efficiently. Dataframes ca
5 min read
Subset Data Frames Using Logical Conditions In R
In this article, we will explore various methods of Subset data frames using logical conditions using the R Programming Language. How to Subset data frames using logical conditionsR language offers various methods to subset data frames using logical conditions. By using these methods provided by R, it is possible to subset a data frame. Some of the
3 min read
How to merge data in R using R merge, dplyr, or data.table
R is a programming language and a software environment that is primarily used for statistic computing and graphics. It provides a wide range of tools for data manipulation, data analysis, data visualization, and statistical modeling. Merging data is a task in data analysis and data manipulation. In R there are various ways to merge data frames, usi
8 min read
Choose Specific Columns of a Data Frame in R Programming - select() Function
select() function in R Language is used to choose whether a column of the data frame is selected or not. Syntax: select(x, expr) Parameters: x: Data frame expr: condition for selection Example 1: # R program to select specific columns # Loading library library(dplyr) # Create a data frame d &lt;- data.frame( name = c(&quot;Abhi&quot;, &quot;Bhavesh
2 min read