How to find common rows between two dataframe in R? - GeeksforGeeks

31 Aug, 2021
In this article, we are going to find common rows between two dataframes in R programming language.
For this, we start by creating two dataframes. Dataframes in use:
We can get the common rows by performing the inner join on the two dataframes. It is available in dplyr() package
Syntax:
inner_join(data1, data2)
where,
data1 is the first dataframe
data2 is the second dataframe
Example: R program to find the common rows in the two dataframes
R
# load the dplyr librarylibrary("dplyr")  # create a dataframe with 3 columns for college1data1 = data.frame(id=c(1, 2, 3),                   name=c("sravan", "ojsawi", "rohith"),                   dept=c("CS", "EC", "IT"))  print(data1)  # create a dataframe with 3 columns for college2data2 = data.frame(id=c(1, 2, 3),                   name=c("ramya", "ojsawi", "rohith"),                   dept=c("Nech", "EC", "IT"))  print(data2)  # get the common rows by using inner joininner_join(data1, data2)
Output:
Example: R program to get the common rows from the dataframes
R
# load the dplyr librarylibrary("dplyr")  # create a dataframe with 3 columns for college1data1 = data.frame(id=c(1, 2, 3, 4, 5),                   name=c("sravan", "ojsawi", "rohith", "bobby", "gnanesh"),                   dept=c("CS", "EC", "IT", "CS", "CS"))  print(data1)  # create a dataframe with 3 columns for college2data2 = data.frame(id=c(1, 2, 3, 4),                   name=c("ramya", "ojsawi", "rohith", "bobby"),                   dept=c("Nech", "EC", "IT", "CS"))  print(data2)  # get the common rows by using inner joininner_join(data1, data2)
Output:
This function is used to get the common rows from the dataframe. This function is available in the generics package so we need to import this package first.
library(“generics”)
Syntax:
generics::intersect(dataframe1, dataframe2)
where
dataframe1 is the first dataframe
dataframe2 is the second dataframe
Example: R program to get the common rows by using intersect() function
R
# load the dplyr librarylibrary("dplyr")  # load generics librarylibrary("generics")  # create a dataframe with 3 columns for college1data1 = data.frame(id=c(1, 2, 3, 4, 5),                   name=c("sravan", "ojsawi", "rohith", "bobby", "gnanesh"),                   dept=c("CS", "EC", "IT", "CS", "CS"))  print(data1)  # create a dataframe with 3 columns for college2data2 = data.frame(id=c(1, 2, 3, 4),                   name=c("ramya", "ojsawi", "rohith", "bobby"),                   dept=c("Nech", "EC", "IT", "CS"))  print(data2)  # get the common rows by using intersectprint(generics: : intersect(data1, data2))
Output:
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
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 Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R