Session Ready
Exercise

Spatial joins

For many analysis types you need to link geographies spatially. For example, you want to know how many trees are in each neighborhood but you don't have a neighborhood attribute in the tree data. The best way to do this is with a spatial join using st_join().

Importantly, the st_join() function requires sf data frames as input and will not accept an object that is just sf geometry. You can use the st_sf() function to convert sf geometry objects to an sf data frame (st_sf() is essentially the opposite of st_geometry()).

Instructions
100 XP
  • Plot the beech trees (beech) on top of the neighborhoods (neighborhoods). You will want to plot only the geometry of the neighborhoods.
  • Use class() to see if the beech object has class data.frame or if it's just geometry.
  • Convert the sf geometry object beech to an sf data frame with st_sf().
  • Use class() to confirm that beech now has a class of data.frame (as well as sf).
  • Use st_join() to conduct a spatial join in order to add neighborhood information to the beech object.
  • Use head() to confirm that the new object has neighborhood information -- for example, it should now have neighborhood name (ntaname).