
Attach an sfc column and class a data frame as sf, without sf
Source: R/sf_availability.R
attach_sfc.Rdmerge(), cbind() and [ dispatch to sf's own methods only when the sf
namespace is loaded. Without it each strips a different part of the sf
representation: merge() loses the class, the sf_column attribute and the
column's own sfc class; cbind() loses the class and the attribute;
subsetting loses the column's class. Attribute-space work therefore happens
with the geometry detached and this is the one place that puts it back.
Details
agr is a named factor over the non-geometry columns with sf's three levels
and no values set, which is what st_as_sf() produces for a frame carrying
no attribute-geometry relationships.
See also
reorder_sfc() to permute the geometry alongside a reordered
attribute table.
Other sf-optional helpers:
reorder_sfc()
Examples
atlas <- dk()
sf_data <- atlas_sf(atlas)
geometry <- sf_data$geometry
# Attribute-space work with the geometry detached, then reattached.
attrs <- sf_data
attrs$geometry <- NULL
class(attrs) <- "data.frame"
attrs$keep <- attrs$label %in% atlas_labels(atlas)[1:5]
rebuilt <- attach_sfc(attrs, geometry)
class(rebuilt)
#> [1] "sf" "data.frame"
inherits(rebuilt$geometry, "sfc")
#> [1] TRUE