I want to make a data frame showing which voting precincts are in each school zone. To do this, I want to see which precincts' center points are in which school zones.


precincts <- st_read("voting_precincts.shp")
precincts$centroid <- st_centroid(
  x = precincts

elemZones <- read_sf("school_zones.shp")

So although I have the centroid coordinates for precincts, I do not know how to "join" them with the school zones data.

1 Answer 1


One of the few limitations simple feature standard for geometries has is that it cannot have two geometry columns; you may have two or more columns with WKT definitions but you have to decide which column to use. In your case you try to add the centroid as a column, but what sf does is to add a the whole sf tbl object (the data frame) as a column, if you print the object with str(precincts) you'll see the classes of each column, if you just print str(precincts$centroid) you'll get:

 Classes ‘sf’, ‘tbl_df’, ‘tbl’ and 'data.frame'

st_centroid will return a whole sf tbl, so you may just do:

 precincts <- st_read("voting_precincts.shp")
 elemZones <- read_sf("school_zones.shp")

 st_centroid(precincts) %>% st_join(elemZones)
  • Worked great. Side note I noticed I used st_read() and read_sf(). That was by mistake. But is there a difference between the two functions?
    – Username
    Commented Nov 16, 2020 at 21:22
  • 2
    read_sf and write_sf are aliases for st_read and st_write, respectively, with some modified default arguments. read_sf and write_sf are quiet by default: they do not print information about the data source. read_sf returns an sf-tibble rather than an sf-data.frame. write_sf delete layers by default: it overwrites existing files without asking or warning. (from the sf package reference)
    – Elio Diaz
    Commented Nov 16, 2020 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.