Extracting data.frame from simple features object in R
To drop the geometry column, use st_drop_geometry()
:
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
nc_df2 <- nc %>% st_drop_geometry()
class(nc_df2)
#> [1] "data.frame"
Before st_drop_geometry()
was added to the sf package (in November, 2018), one could produce the same result using the st_set_geometry()
function, like this:
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
class(nc)
#> [1] "sf" "data.frame"
nc_df <- nc %>% st_set_geometry(NULL)
class(nc_df)
#> [1] "data.frame"
Set the st_geometry
property to NULL
.
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
class(nc)
## [1] "sf" "data.frame"
st_geometry(nc) <- NULL
class(nc)
## [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)
as.data.frame(nc)