hier_grid: Compute a grid given different hierarchies

View source: R/hier_grid.R

hier_gridR Documentation

Compute a grid given different hierarchies

Description

This function returns a data.table containing all possible combinations of codes from at least one hierarchy object. This is useful to compute a "complete" table from several hierarchies.

Usage

hier_grid(..., add_dups = TRUE, add_levs = FALSE, add_default_codes = FALSE)

Arguments

...

one or more hierarchy objects created with hier_create() or hier_compute()

add_dups

scalar logical defining if bogus codes (codes that are the only leaf contributing to a parent that also has no siblings) should be included.

add_levs

scalar logical defining if numerical levels for each codes should be appended to the output data.table.

add_default_codes

scalar logical definining if standardized level codes should be additionally returned

Value

a data.table featuring a column for each hierarchy object specified in argument .... These columns are labeled ⁠v{n}⁠. If add_levs is TRUE, for each hierarchy provided, an additional column labeled ⁠levs_v{n}⁠ is appended to the output. Its values define the hierarchy level of the corresponding code given in ⁠v{n}⁠ in the same row. If add_default_codes is TRUE, for each hierarchy provided an additional column ⁠default_v{n}⁠ is provided

Examples

# define some hierarchies with some "duplicates" or "bogus" codes
h1 <- hier_create("Total", nodes = LETTERS[1:3])
h1 <- hier_add(h1, root = "A", node = "a1")
h1 <- hier_add(h1, root = "a1", node = "aa1")

h2 <- hier_create("Total", letters[1:5])
h2 <- hier_add(h2, root = "b", node = "b1")
h2 <- hier_add(h2, root = "d", node = "d1")

# with all codes, also "bogus" codes
hier_grid(h1, h2)

# only the required codes to build the complete hierarchy (no bogus codes)
hier_grid(h1, h2, add_dups = FALSE)

# also contain columns specifying the hierarchy level
hier_grid(h1, h2, add_dups = FALSE, add_levs = TRUE)

sdcHierarchies documentation built on May 29, 2024, 8:01 a.m.