Get startedGet started for free

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

View Course

Exercise instructions

  • Use merge(), passing the spatial object nyc_tracts first and the data frame nyc_income second. Specify by.x = "TRACTCE" and by.y = "tract". Store the result in nyc_tracts_merge.
  • Use summary() on nyc_tracts_merge to verify the object is a SpatialPolygonsDataFrame and the data also contain the needed estimate column from nyc_income.
  • Use tm_shape() and add a tm_fill() layer to create a choropleth map of nyc_tracts_merge, mapping color to estimate.

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
Edit and Run Code