Merging data attributes
merge() by default merges based on columns with the same name in both datasets. In your case, this isn't appropriate since the column of IDs is called tract in one dataset and TRACTCE in the other. To handle this, merge() has the optional arguments by.x and by.y, where you can specify the names of the column to merge on in the two datasets, respectively.
merge() returns a new Spatial___DataFrame object, so you can take a look at the result by plotting it with tmap.
Let's go ahead and merge.
This exercise is part of the course
Visualizing Geospatial Data in R
Exercise instructions
- Use
merge(), passing the spatial objectnyc_tractsfirst and the data framenyc_incomesecond. Specifyby.x = "TRACTCE"andby.y = "tract". Store the result innyc_tracts_merge. - Use
summary()onnyc_tracts_mergeto verify the object is aSpatialPolygonsDataFrameand the data also contain the neededestimatecolumn fromnyc_income. - Use
tm_shape()and add atm_fill()layer to create a choropleth map ofnyc_tracts_merge, mapping color toestimate.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
library(sp)
library(tmap)
# Merge nyc_tracts and nyc_income: nyc_tracts_merge
# Call summary() on nyc_tracts_merge
# Choropleth with col mapped to estimate