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.
Este exercício faz parte do curso
Visualizing Geospatial Data in R
Instruções do exercício
- 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.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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