Session Ready
Exercise

Join spatial and non-spatial data

In this exercise you will test joining spatial and non-spatial data. In particular, the trees data you have been working with has a full county name (the variable is called boroname) but does not have the county codes. The neighborhoods file has both a county name (the variable is called boro_name) and the county codes -- neighborhoods are nested within counties. In this exercise, you will create a non-spatial data frame of county name and county code from the neighborhoods object. Then you will join this data frame into the spatial trees object with inner_join().

Instructions
100 XP

The packages sf and dplyr and the objects neighborhoods and trees are loaded in your workspace.

  • Use the select() function from dplyr to limit the neighborhoods dataset to the fields boro_name, county_fip and boro_code and save this new object as boro.
  • Drop the geometry column from boro with st_set_geometry() and assign it to boro_no_geometry.
  • Use the dplyr function distinct() to limit the dataset to just one occurrence of each county/boro (otherwise the county information is repeated for each neighborhood).
  • Join the newly created boro_distinct data frame into the trees object with inner_join() from dplyr.
  • Use head() to confirm that the new fields (county_fip and boro_code) are included in the trees object.