Session Ready
Exercise

The wrong way

When a Spatial***DataFrame object is created, there are two ways the spatial objects (e.g. Polygons) might be matched up to the rows of the data. The most robust is to use IDs on the spatial objects that are matched up to row names in the data. This ensures if there are any that don't match you are quickly alerted. The other way is simply by order -- the first spatial object is assumed to correspond to the first row of data.

Once created, the correspondence is based purely on order. If you manipulate the data slot, there is no checking the spatial objects still correspond to the right rows. What does this mean in practice? It's very dangerous to manipulate the data slot directly!

To create your plot of income, you need to match up the income data frame with the tracts SpatialPolygonsDataFrame. To illustrate the danger of manipulating @data directly, let's see what happens if you try to force nyc_income in to nyc_tracts.

Instructions 1/2
undefined XP
  • 1
  • 2
  • Use str() to look at nyc_income.
  • Do the same for the data slot of nyc_tracts.

They both have the same number of rows, with information about the same tracts (tract in nyc_income and TRACTCE in nyc_tracts), but in different orders.