check_groups() tests a grouping of the units of a layer and returns
one row per violation. It checks a result of group_units(), a
grouping edited by hand, or a published one, since it reads only the
group ids and the graph.
Usage
check_groups(
x,
group,
size,
min = NULL,
max = NULL,
max_area = NULL,
within = NULL,
fixed = NULL,
cross_below = 0,
n = NULL,
n_by = NULL,
groups = NULL,
graph = NULL
)Arguments
- x
An
sfobject (orsfc) of polygons in a projected CRS, one row per unit, or itsunit_graph(). A layer gets the graphunit_graph(x, within = within)unlessgraphis given. Pass the graph the groups were built from when it has barriers or patches.- group
The group of each unit, any atomic vector in the row order of the layer, or a column name of
xwhenxis a layer.NAmarks an unassigned unit.- size
The size of each unit, a nonnegative numeric vector in the row order of the layer, or a column name of
xwhenxis a layer.- min, max
The smallest and largest size a group may have, as a number, one value per unit that is the same within each part of
within, or a column name ofxwhenxis a layer, orNULL(default) for no bound.NAinminmeans no minimum for that part. Each group is checked against the bounds of its part.- max_area
The largest area a group may have, in the squared units of the graph's CRS, or
NULL(default) for none.- within
The part of the layer each unit lies in, as given to
group_units(). For a layer only, since a graph carries its parts.- fixed
The locked groups the grouping was asked to keep, as given to
group_units(), orNULL.- cross_below
As given to
group_units(), a number or one value per unit, so that the same barrier crossings are opened.- n, n_by, groups
As given to
group_units(), orNULL(default), so that the crossings a count opens are opened here too and the groups they join are not reported as disconnected. Withn, the number of groups of each level is checked, and withgroups, the groups are checked against the rows of the table.- graph
The
unit_graph()of the layerx, orNULL(default). With it, the columns ofxcan be named, and the graph gives the parts, sowithinis left out. The call then reads like that ofgroup_metrics(), and gives the result of the graph passed asx. It must be built fromxitself, in the same row order. Only its number of units is checked, so a graph of a reordered or edited layer gives wrong results without an error.
Value
A data frame with one row per violation, and no rows when the grouping meets every rule. Its columns are
check, the rule broken (see Details).group, the group concerned.units, its number of units.size, its size.forced,TRUEwhen the caps or the locked groups make the violation unavoidable, such as a single unit abovemax, andNAfor the checks ofnandgroups, which no cap explains.detail, the level, part or row a check ofnorgroupsconcerns, andNAfor the other checks.
Details
A grouping meets these rules, each reported under its own name.
"unassigned", every unit has a group."disconnected", the units of a group are connected through the edges of the graph, including the barrier crossings and gap links thatcross_belowandnopen and those between two units of one locked group (seegroup_units())."not_within", a group lies in one part ofwithin."over_max"and"over_max_area", a group stays within the caps. The violation is forced when the group is one unit or one locked group, or, for"over_max", when one unit holds all of the group's size."below_min", a group reachesmin. The violation is forced when no neighboring group could absorb it within the caps, or when it is locked."patch_split", a patch that fits under the caps, or any patch when no cap is given, lies in one group."locked_changed", a locked group keeps exactly its units."count", withn, each level holds its count of groups. A row gives the units and size of the level, withNAingroup."unknown_group", withgroups, each group is theidof a row of the table, or its row number when the table has noid."no_group", withgroups, each row of the table has a group. A row gives itsidor row number ingroup, with no units."wrong_part", withgroups, each group lies in the part of its row.
A group cannot cross a barrier, since the graph has no edge across it,
so a group that does is reported as "disconnected". Size is conserved
by any vector of group ids, so that rule is checked on dissolved layers
instead, with dissolve_units().
With n, a group below min is often the count at work rather than a
fault, where a part holds too little for its count of groups at min
each, or where its size sits in a few large patches. Such a group
is not marked forced, since a trade with a larger neighbor could still
lift it. When the small groups are real, such as the small EAs of a
census table, give the table as groups and leave min out, so each
group is cut toward its own size.
See also
Other checks:
audit_field(),
check_units(),
graph_lines(),
group_metrics()
Examples
cells <- sf::st_make_grid(
sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 400, ymax = 300)),
cellsize = 100
)
cells <- sf::st_sf(geometry = cells, crs = 32630)
cells$pop <- rep(c(50, 120, 80, 30), 3)
g <- unit_graph(cells)
# Two opposite corners in one group: disconnected
bad <- seq_len(nrow(cells))
bad[12] <- 1
check_groups(g, bad, cells$pop, max = 200)
#> check group units size forced detail
#> 1 disconnected 1 2 80 FALSE <NA>