First, you need to read in both your data sets:
Next comes the merge. You can use by.x and by.y to declare which variables the merge will be based on. If the variables have exactly the same name in both data sets, you can use by instead of by.x and by.y.
x and y represent the two data sets you are merging, in that order.
You also want to state whether you want to include all data from either data set, using all or all.x and all.y. In this case, we want to make sure we hold onto all our city data, even data for the cities we do not have coordinates for.
Running that code shows what we would expect. Houston is included in the final data set even though there are no coordinates for it, while Dallas is not included since it has coordinates but no data:
BonusIf you'd like to get a list of which cases got merged in but lack coordinate data, there's a simple line of code to do that:
Also, you might want to tidy up the names of your variables, if they followed different conventions in their respective initial data sets:
The full sample code is available as a gist.
Change R code tabs to 2 spaces.
bold package names
for R posts, use: sessionInfo()