Given two data frames as example:
df1 = data.frame(V1 = c('JOHN', 'BRIAN','KATE', 'ERIC', 'CURT', 'ZACH'), V2 = c('ABIGAIL', 'ANDY', 'GEORGE', 'JOHN', 'MARY', 'FRANKLIN'), V3 = seq(1,6,1), V4 = seq(1,6,1))
df2 = data.frame(V1 = c('ABIGAIL', 'BRIAN','KATE', 'ERIC'), V2 = c('JOHN', 'ANDY', 'MARTIN', 'ANDREW'))
df1
V1 V2 V3 V4
JOHN ABIGAIL 1 1
BRIAN ANDY 2 2
KATE GEORGE 3 3
ERIC JOHN 4 4
CURT MARY 5 5
ZACH FRANKLIN 6 6
df2
V1 V2
ABIGAIL JOHN
BRIAN ANDY
KATE MARTIN
ERIC ANDREW
How can I create a new df3
containing the rows that match between columns V1
and V2
in df1
and df2
. The challenge is that I want this match independently of the order.
So that df3
would in the example look like:
df3
V1 V2 V3 V4
ABIGAIL JOHN 1 1
BRIAN ANDY 2 2
I have tried using match() and the %in% operator without any luck.
I am looking for a solution applicable a data frame with several thousand rows.
EDIT
Both answers gave a working solution to my question. It turns out I missed something to make it applicable to my own data. Instead given the data frames:
df1 = data.frame(V1 = c('JOHN', 'BRIAN','KATE', 'ERIC', 'CURT', 'ZACH'), V2 = c('ABIGAIL', 'ANDY', 'GEORGE', 'JOHN', 'MARY', 'FRANKLIN'), V3 = seq(1,6,1), V4 = seq(1,6,1))
df2 = data.frame(V1 = c('ABIGAIL', 'BRIAN','KATE', 'BRIAN', 'ERIC'), V2 = c('JOHN', 'ANDY', 'MARTIN', 'ANDY', 'ANDREW'))
Now I also want to extract the rows even if the match between them are not in the same row. df3
would then look like:
df3
V1 V2 V3 V4
ABIGAIL JOHN 1 1
BRIAN ANDY 2 2
BRIAN ANDY 2 2
Basically I want the values in V3
and V4
from df1
for every interactions between the names in V1
and V2
independently of the order.