The two public halves of the from -> leaves -> to route:
recast_to_geoleaves() projects region-keyed data DOWN to the leaf layer
(one row per leaf), and recast_from_geoleaves() aggregates leaf-keyed
data UP into a geoframe's regions. Their composition is
recast_geoscale(), and because the leaf rows are keyed by the leaf
codes, recast_from_geoleaves(recast_to_geoleaves(x, gs_a), gs_b, to) recasts across two different GeoScales that share leaf codes.
Usage
recast_to_geoleaves(
data,
gs,
source = NULL,
key = NULL,
values = NULL,
rule = NULL,
weight = NULL,
attach_weight = TRUE,
collect = NULL,
from = lifecycle::deprecated()
)
recast_from_geoleaves(
data,
gs,
target,
key = NULL,
values = NULL,
rule = NULL,
weight = NULL,
data_weight = NULL,
na_rm = FALSE,
na_action = c("drop", "error", "keep"),
collect = NULL,
to = lifecycle::deprecated()
)Arguments
- data
The data: for
recast_to_geoleaves()keyed by region code at geoframesource; forrecast_from_geoleaves()keyed by leaf codes.- gs
The
GeoScalethe data is keyed in (to_geoleaves) or aggregated into (from_geoleaves).- source
to_geoleavesonly: geoframe name the codes belong to.NULL(default) is inferred as inrecast_geoscale().- key
The key column.
to_geoleaves: defaults tosourcewhen that column exists, otherwise the GeoScale's leaf column.from_geoleaves: the GeoScale's leaf column.- values, rule
As in
recast_geoscale().- weight
A declared weight column of
gs, as inrecast_geoscale().from_geoleavesuses it only whendatacarries no weight of its own.- attach_weight
to_geoleavesonly: attach theweightcolumn (defaultTRUE).- collect
For lazy inputs: materialise (
TRUE) or return the query (default).- from, to
Deprecated names of
sourceandtarget.- target
from_geoleavesonly: target geoframe name.- data_weight
from_geoleavesonly: the name of a column ofdatato weight by, so the weight may vary by identifier – a capacity-weighted efficiency differs by year and vintage.NULL(default) uses aweightcolumn ofdataif present, else the scale weightweight.- na_rm
from_geoleavesonly: read anNAvalue as "this member says nothing" rather than as an unknown that makes the whole groupNA(defaultFALSE). Only an all-NAgroup staysNA; a weighted mean drops the weight of eachNAmember so the divisor still matches.- na_action
from_geoleavesonly: what to do with leaves that have no code attarget–"drop"(default, warning),"error", or"keep"(anNAregion row).
Value
recast_to_geoleaves(): one row per (leaf x identifier
combination) with the leaf column (under its own name in the GeoScale,
see geoscale_leaf_column()), identifiers, values (and weight).
recast_from_geoleaves(): one row per (region x identifier
combination) with columns target-named region, identifiers, values.
Both in the input's class; lazy in, lazy out.
Details
Going down, extensive columns (rule "sum") are split across a region's
leaves proportionally to the chosen weight so totals conserve; intensive
columns are repeated. A weight column (the leaf's weight) is attached
by default so that the return trip's "weighted_mean" reproduces the
source weighting exactly; pass attach_weight = FALSE to omit it.
Going up, rules act on the leaf rows directly: "sum" sums, "mean"
averages, "weighted_mean" uses the weight column when present (else
the target object's chosen weight), "copy" requires constancy, "sd"
is the standard deviation over the leaves.
Both ends run as dplyr pipelines and accept any supported backend (see
recast_geoscale()'s Backends section); the geoscale side of every
join is a small in-memory frame.
Examples
gs <- geoscale_example()
y <- data.frame(country = c("N", "S"), capacity = c(10, 20))
a <- recast_to_geoleaves(y, gs, source = "country", rule = "sum",
weight = "km2")
a
#> cell capacity weight
#> 1 A1 1.000000 100
#> 2 A2 2.000000 200
#> 3 A3 3.000000 300
#> 4 A4 4.000000 400
#> 5 A5 9.090909 500
#> 6 A6 10.909091 600
sum(a$capacity) # 30 -- totals conserve
#> [1] 30
recast_from_geoleaves(a, gs, target = "state", rule = "sum")
#> state capacity
#> 1 N1 3
#> 2 N2 7
#> 3 S1 20
