Adds a label column named after the scale, and optionally the codes of coarser frames ("<name>.<frame>") and the unit's share and weight ("<name>.share", "<name>.weight").

join_scale(
  data,
  x,
  key = NULL,
  frame = NULL,
  attach = NULL,
  meta = FALSE,
  weight = NULL,
  as_factor = TRUE,
  collect = NULL,
  diagnostics = c("auto", "on", "off")
)

Arguments

data

The data, in any supported backend, with a column of unit codes.

x

The NestedScale to attach.

key

Name of the code column in data. Inferred from the scale's name, the keyed frame, or the scale's own key column.

frame

The frame the codes in key are at. NULL (default) is inferred as the single frame name appearing among the data's columns.

attach

Coarser frames whose codes to attach as new columns: TRUE for all of them, a character vector to choose, NULL/FALSE for none.

meta

Attach the unit's share and weight at the keyed frame.

weight

Weight column used for meta. NULL uses the default.

as_factor

Return attached membership columns as factors with the frame's full vocabulary as levels (so empty groups still plot).

collect

For lazy inputs: materialise (TRUE) or return the query.

diagnostics

Whether to run the checks that read the data (no matching codes; codes that are not units). "auto" (default) runs them on eager inputs and skips them on arrow/dtplyr, which makes this verb zero-scan there; "on"/"off" force the choice.

Value

data with the attached columns, in its own class.

Details

Existing columns are never overwritten – a collision is an error.

Examples

s <- scale_example()
d <- data.frame(class = c("G1", "G2", "S1"), v = 1:3)
join_scale(d, s, attach = TRUE)
#>   class v example.sector example
#> 1    G1 1              P      G1
#> 2    G2 2              P      G2
#> 3    S1 3              S      S1
join_scale(d, s, attach = "sector")
#>   class v example.sector example
#> 1    G1 1              P      G1
#> 2    G2 2              P      G2
#> 3    S1 3              S      S1
join_scale(d, s, meta = TRUE)
#>   class v example.weight example.share example
#> 1    G1 1            300     0.1428571      G1
#> 2    G2 2            700     0.3333333      G2
#> 3    S1 3           1100     0.5238095      S1