How to find common rows between two dataframe in R?

The appropriate dplyr function here is inner_join (returns all rows from df x that have a match in df y.)

library(dplyr)
inner_join(df1, df2)

      V1
1  id300
2 id5456
3   id45

Note: the rows are returned in the order in which they are in df1. If you did inner_join(df2, df1), id45 would come before id5456.


Use merge

new_data_frame <- merge(data.frame1, data.frame2)

I'm assuming you have only one column in each data frame and they have the same name in both frames. If not use the column you want to intersect by with by.x = "nameCol1" and by.y = "nameCol2", where nameCol are the real column names.

Added after first comment
If you have more columns in any data frame the command is the same. Do it this way:

>a  #Data frame 1
      c1 c2
1  id300  6
2 id2345  5
3 id5456  4
4   id33  3
5   id45  2
6   id54  1

> b #Data frame 2
     a  f
1  asd 12
2 id33 10
3 id45  8
4 id54  6

As you may see, they don't share column names and have 2 columns each. So:

> merge(a,b, by.x = "c1", by.y = "a")

    c1 c2  f
1 id33  3 10
2 id45  2  8
3 id54  1  6

The only rows that are left are those that have the same entries in common in the selected columns.


common <- intersect(data.frame1$col, data.frame2$col)  
data.frame1[common,] # give you common rows in data frame 1  
data.frame2[common,] # give you common rows in data frame 2

Tags:

R