Skip to contents

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 sf object (or sfc) of polygons in a projected CRS, one row per unit, or its unit_graph(). A layer gets the graph unit_graph(x, within = within) unless graph is 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 x when x is a layer. NA marks 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 x when x is 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 of x when x is a layer, or NULL (default) for no bound. NA in min means 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(), or NULL.

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(), or NULL (default), so that the crossings a count opens are opened here too and the groups they join are not reported as disconnected. With n, the number of groups of each level is checked, and with groups, the groups are checked against the rows of the table.

graph

The unit_graph() of the layer x, or NULL (default). With it, the columns of x can be named, and the graph gives the parts, so within is left out. The call then reads like that of group_metrics(), and gives the result of the graph passed as x. It must be built from x itself, 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, TRUE when the caps or the locked groups make the violation unavoidable, such as a single unit above max, and NA for the checks of n and groups, which no cap explains.

  • detail, the level, part or row a check of n or groups concerns, and NA for 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 that cross_below and n open and those between two units of one locked group (see group_units()).

  • "not_within", a group lies in one part of within.

  • "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 reaches min. 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", with n, each level holds its count of groups. A row gives the units and size of the level, with NA in group.

  • "unknown_group", with groups, each group is the id of a row of the table, or its row number when the table has no id.

  • "no_group", with groups, each row of the table has a group. A row gives its id or row number in group, with no units.

  • "wrong_part", with groups, 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.

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>