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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With