I would like to make a new data frame which only includes common rows of two separate data.frame. example:
data.frame 1
1 id300
2 id2345
3 id5456
4 i
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.