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?
Try:
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:
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.