How to Merge DataFrames Based on Multiple Columns in R? - GeeksforGeeks

28 Nov, 2021
In this article, we will discuss how to merge dataframes based on multiple columns in R Programming Language.
We can merge two  dataframes based on multiple columns  by using merge() function
Syntax:
merge(dataframe1, dataframe2, by.x=c(‘column1’, ‘column2’...........,’column n’), by.y=c(‘column1’, ‘column2’...........,’column n’))
where
dataframe1 is the first dataframe
dataframe2 is the second dataframe
by.x represents first dataframe columns
by.y represents second dataframe columns
Let create two dataframes and display
Example: R program to create two dataframes with 4 columns of student data
R
# create dataframe1data1 = data.frame(name=c("manoj", "manoja", "saroja", "ramya"),                   subjects=c("java", "c/cpp", "python", "R"),                   city=c("ponnur", "tenali", "hyd", "guntur"),                   marks=c(89, 90, 78, 89))  # create dataframe2data2 = data.frame(name=c("manoj", "sravya", "saroja", "pavani"),                   subjects=c("java", "c/html", "python", "php/css"),                   city=c("ponnur", "hyd", "hyd", "guntur"),                   marks=c(89, 78, 78, 81))  # displayprint(data1)print(data2)
Output:
Example 1 : Merge dataframes on 2 columns
R
# create dataframe1data1 = data.frame(name=c("manoj", "manoja", "saroja", "ramya"),                   subjects=c("java", "c/cpp", "python", "R"),                   city=c("ponnur", "tenali", "hyd", "guntur"),                   marks=c(89, 90, 78, 89))  # create dataframe2data2 = data.frame(student=c("manoj", "sravya", "saroja", "pavani"),                   subjects=c("java", "c/html", "python", "php/css"),                   city=c("ponnur", "hyd", "hyd", "guntur"),                   exams=c(89, 78, 78, 81))  # merge dataframes based on name and subjects from data1# with student and subjects from data2merge(data1, data2, by.x=c('name', 'subjects'),      by.y=c('student', 'subjects'))
Output:
Example 2: Merge the dataframes based on three columns.
R
# create dataframe1data1 = data.frame(name=c("manoj", "manoja", "saroja", "ramya"),                   subjects=c("java", "c/cpp", "python", "R"),                   city=c("ponnur", "tenali", "hyd", "guntur"),                   marks=c(89, 90, 78, 89))  # create dataframe2data2 = data.frame(student=c("manoj", "sravya", "saroja", "pavani"),                   subjects=c("java", "c/html", "python", "php/css"),                   city=c("ponnur", "hyd", "hyd", "guntur"),                   exams=c(89, 78, 78, 81))  # merge dataframes based on name,city and subjects from data1# with student,city and subjects from data2merge(data1, data2, by.x=c('name', 'subjects', 'city'),      by.y=c('student', 'subjects', 'city'))
Output:
Picked
R DataFrame-Programs
R-DataFrame
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to import an Excel File into R ?
How to filter R DataFrame by values in a column?
Time Series Analysis in R
R - if statement
Logistic Regression in R Programming