Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two data frames in R and find common values and non-matching values

Tags:

dataframe

r

match

I am trying to find a function to match two data frames of different lengths on one common column only, and create a different column which specifies if it found a match or not. So, for example, df1 is:

Name Position location
francesca A 75
cristina B 36

And df2 is:

location Country
75 UK
56 Austria

And I would like to match on "Location" and the output to be something like:

Name Position Location Match
francesca A 75 1
cristina B 36 0

I have tried with the function match or with:

subset(df1, location %in% df2)

But it does not work.

Could you please help me figure out how to do this?

like image 252
user971102 Avatar asked Oct 03 '11 18:10

user971102


People also ask

How do I merge two datasets with common variable in R?

To join two data frames (datasets) vertically, use the rbind function. The two data frames must have the same variables, but they do not have to be in the same order. If data frameA has variables that data frameB does not, then either: Delete the extra variables in data frameA or.

How do I join two Dataframes with common column in R?

The merge() function in base R can be used to merge input dataframes by common columns or row names. The merge() function retains all the row names of the dataframes, behaving similarly to the inner join. The dataframes are combined in order of the appearance in the input function call.

Can you combine data sets in R?

When you have multiple datasets that have the same set of columns, you can concatenate one dataset to another, vertically. That is, keeping the columns of your dataset, you can add more rows to it.


1 Answers

Try:

df1$match <- match(df1$location, df2$location, nomatch=0)

This will add a column to df1 indicating which row in df2 matches it (considering only location as you specified). If there are no matches, zero will be returned, so you get:

> df1
       Name Position location match 
1 francesca        A       75     1
2  cristina        B       36     0

One caution: if there are multiple matches in the second table, you'd want to use a different approach as this method only returns the first match. I assume these are unique because of the way you specified your question, so this should not be an issue.

like image 65
Jason B Avatar answered Sep 28 '22 19:09

Jason B