Aggregate along spatial dimension
spatial_aggregate.Rd
Takes geographic data (points or polygons), and aggregates into polygons,
while retaining theme-level information. This function sets up the data with
specific prep for the way the spatial dimension works, and then wraps
general_aggregate()
. Many of the arguments here are just passed through to
general aggregate.
Usage
spatial_aggregate(
dat,
to_geo,
groupers,
aggCols,
funlist,
...,
whichcrs = sf::st_crs(to_geo),
keepAllPolys = FALSE,
failmissing = TRUE,
prefix = "spatial_",
joinby = "spatial",
auto_ewr_PU = FALSE
)
Arguments
- dat
sf of values to aggregate with any necessary non-spatial grouping information (e.g. scenario, theme)
- to_geo
sf polygon or multipolygon that provides the desired spatial level to group into. This uses the intersection
sf::st_intersection()
, so ifdat
andto_geo
are both polygons, they do not have to be nested.- groupers
as in
general_aggregate()
, with the note that these should be all grouping columns except the polygons into_geo
, which are automatically added togroupers
before passing togeneral_aggregate()
.- aggCols
an expression for the columns to aggregate (the data columns). See
selectcreator
for formats- funlist
a list of functions and their arguments used to aggregate the data. See
functionlister
for creation in many cases. The situation with a bare anonymous function, e.g.~mean(., na.rm = T)
is not supported because we need a name. Use a named list if using anonymous functions, e.g.list(mean = ~mean(., na.rm = T))
. If using functions with a data-variable argument, e.g. weighted.mean with a column of weights, we now (as ofdplyr
1.1) have some workarounds. One option is if specified as a function argument, the function can just go in as a bare name or anonymous. If specified elsewhere, it can be wrapped inrlang::quo()
, e.g.agglist <- rlang::quo(list(mean = mean, wm = ~weighted.mean(., weight_column_name, na.rm = T)))
. If it isn't, there is now an internal workaround to add that on that seems to be stable but may cause unforeseen issues. This workaround also allows building custom aggregation functions (not-anonymous) with the data-variable argument either exposed or hardcoded (seeSpatialWeightedMean()
). The error checks for names do not work for quosures, so make sure you name the list if usingrlang::quo()
.- ...
arguments passed to the aggregation functions. This is very limited, and does not work with data arguments under most conditions. Almost always better to specify explicitly when building
funlist
, but works OK with simple functions, e.g. passingna.rm = TRUE
to mean- whichcrs
desired coordinate reference system, easiest is just the numeric EPSG code, but could a full crs definition. See
sf::st_crs()
- keepAllPolys
logical, default
FALSE
. Should polygons into_geo
that have no values indat
be retained? The defaultFALSE
keeps NA polygons from cluttering things up, butTRUE
can be useful to not lose them, especially for later plotting. However, it is typically best from a data and cleanliness perspective to useFALSE
here and use the bare set of polys as anunderlay
inplot_outcomes()
.- failmissing
logical, default
TRUE
: fail if the requested grouping or aggregation columns not exist. IfFALSE
, proceed with those that do exist and silently drop those that don't. Similar totidyselect::all_of()
vstidyselect::any_of()
intidyselect
- prefix
character, differs from
general_aggregate()
in that default is'spatial_'
instead of'agg_'
.- joinby
character, default 'spatial' performs the expected spatial join using geometry, 'nonspatial' performs a
dplyr::left_join()
by common column names, typically as a result of callingmulti_aggregate()
withpseudo_spatial = 'planning_units'
.- auto_ewr_PU
logical, default FALSE. If TRUE, automatically infers whether this is an EWR dataset is undergoing gauge to sdl or planning unit aggregation. If so, joins data non-spatially (sets
joinby = 'nonspatial'
). The preferred solution is to usejoinby
inspatial_aggregate()
orpseudo_spatial
inmulti_aggregate()
. If none of those solutions happen, though, it aborts to prevent incorrectly spatial joining of gauges to planning units.