Session Ready
Exercise

Dropping geometry from a data frame

One of the great innovations of sf over sp is the use of data frames for storing spatial objects. This allows you to slice and dice your spatial data in the same way you do for non-spatial data. This means you can, for example, apply dplyr verbs directly to your sf object.

One important difference between dplyr with and without spatial data is that the resulting data frames will include the geometry variable unless you explicitly drop it. If you want to force the geometry to be dropped you would use the sf function st_set_geometry() and you would set the geometry to NULL.

Instructions
100 XP

The packages sf and dplyr, and the object trees are loaded in your workspace.

  • Use the dplyr function count() to tally the number of trees by species from the trees dataset. Sort in decreasing order of n by setting sort to TRUE, and call this new data frame species_counts.
  • Use the head() function to check if your new data frame has a geometry column.
  • Drop the geometry column from species_counts using st_set_geometry().
  • Confirm the geometry column has been dropped by re-running head().