The issue is that the CITY column in the crime dataframe does not have any matching values with the CITY column in the district dataframe. As a result, when you try to join these two datasets using the CITY column as the key, R returns an empty character vector (character(0)).
On the other hand, the COUNTY column in both datasets has some matching values, which is why the intersection of COUNTY columns returns a single county name (“adams county”).
To resolve this issue, you need to identify the common columns between the two datasets that can be used as keys for joining. In this case, it seems that only the COUNTY column has matching values in both datasets.
Here’s an example of how you could modify your join statement to only include the matching columns:
crime <- inner_join(crime, district, by = c("CITY" = "CITY", "COUNTY" = "COUNTY"))
Alternatively, you can also use the merge() function instead of inner_join(), which allows you to specify the join type (e.g. inner, left, right) and the columns to join on:
crime <- merge(crime, district, by.x = c("CITY", "COUNTY"), by.y = c("CITY", "COUNTY"), all.x = TRUE)
Note that I’ve used the all.x = TRUE argument to include all rows from the crime dataframe in the resulting merged dataset, even if there are no matches in the district dataframe.
Last modified on 2024-06-18