I have two dataframes in R I am working with. Many many lines of actual data.
data source, lots of lines of data with many locations that are the same but with different values in Num_Horses column: df1:
Location | Code | Num_Horses |
---|---|---|
Rocky Point | 112 | 65 |
Clear Bay | 114 | 32 |
Port Safety | 115 | 22 |
Sandy Beach | 112 | 14 |
And
location reference guide, which is a table with all the known place names in the area, and their corresponding attributes: refdf:
Location | Code | SubCode | County | District |
---|---|---|---|---|
Rocky Point | 112 | 12 | red | 5584 |
Clear Bay | 114 | 22 | nelson | 5546 |
Port Safety | 115 | 13 | grip | 5594 |
Sandy Beach | 112 | 14 | red | 5523 |
I would like use df1$Location to reference refdf and add new columns (Code, Subcode, County, District) to df1, while retaining other columns in df1. I want the end product to look something like the below:
Location | Code | SubCode | County | District | Num_Horses |
---|---|---|---|---|---|
Rocky Point | 112 | 12 | red | 5584 | 65 |
Clear Bay | 114 | 22 | nelson | 5546 | 32 |
Port Safety | 115 | 13 | grip | 5594 | 22 |
Sandy Beach | 112 | 14 | red | 5523 | 14 |
Any recommendations on code for this code? Thanks!
I am looking for a starting point, because I am unsure on how to write a logical statement something like:
IF df1$Location = refdf$location, THEN assign df1 row of data corresponding Code, Subcode, County, and District from refdf.