Is there an sf-native (i.e. "correct") way of extracting everything except the geometry column from a simple features object? This works

df <- dplyr::select(as.data.frame(sf), -geometry)

but the select( , -geometry) step feels unnecessary. Also, it doesn't remove the geometry attributes.


Set the st_geometry property to NULL.

nc <-  st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
## [1] "sf"         "data.frame"
st_geometry(nc) <- NULL
## [1] "data.frame"

Also (though this won't remove the attr(nc, "sf_column"):

nc <-  st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
  • I had tried nc$geometry <- NULL before, but your suggestion did the trick. Thank you. – eivindhammers Jan 18 '17 at 12:43
  • is there a piped version of this? sf %>% mutate(geometry = NULL) doesn't work – obrl_soil Apr 25 '17 at 0:52
  • 2
    Maybe it's called "geom" and not "geometry"? (It's never going to be constant). Another way is ` x %>% st_set_geometry(NULL)`. Either of these might be in dev, not CRAN though I'm not checking carefully where I'm at locally. – mdsumner Apr 25 '17 at 2:03
  • @mdsumner makes a great, indirect, point. Somebody has to actually write the method, it does not happen by magic. Since sf is a new object class in R many methods may not be available or not behave as expected. – Jeffrey Evans Dec 5 '17 at 15:49

Here is a pipeable approach to transform an sf object into a simple data frame with no geometry. Use the st_set_geometry() function:

nc <-  st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
#> [1] "sf"         "data.frame"

nc_df <- nc %>% st_set_geometry(NULL)
#> [1] "data.frame"
df <- df[,-geometry_column]

Or just df[,-geometry_column] if you don't want to create new object


df - your object

geometry_column - column where geometry is stored

  • Doesn't work on my end. Have you tested it? I do sf[, -sf$geometry] and get Error in Ops.sfc(grid.sf$geometry) : argument "e2" is missing, with no default – eivindhammers Jan 18 '17 at 11:30
  • Try sF[,-geometry] not sf[,-sf$geometry] – ami Jan 18 '17 at 11:34
  • sf[, -geometry] fails with Error in -"geometry" : invalid argument to unary operator. Reproduce with data(meuse, package = "sp"); meuse_sf <- st_as_sf(meuse); meuse_sf[, -geometry] – eivindhammers Jan 18 '17 at 11:38
  • 5
    This does not work in any way. Maybe it did in some earlier sf package, but now it doesn't. All subsetting of sf data frames preserves the geometry column. Please delete. – Spacedman Jan 5 '18 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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