This function returns a data.table containing all possible combinations of codes from one or more hierarchy objects. This is useful to compute "complete" tables for SDC purposes.

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

Arguments

...

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

add_dups

scalar logical defining if bogus codes (parents with only one child and no siblings) should be included. If FALSE, these parents are removed and replaced by their most granular leaf.

add_levs

scalar logical defining if numerical levels for each code should be appended to the output.

add_default_codes

scalar logical defining if standardized level codes should be additionally returned.

add_contributing_cells

logical: if TRUE, two columns are added: leaf_id (a unique integer for terminal leaf combinations) and contributing_leaf_ids (a list-column of integers representing all terminal leaves contributing to that cell).

Value

a data.table featuring:

  • v{n}: columns for each hierarchy object.

  • cell_id: a unique string identifier created by concatenating default codes.

  • levs_v{n}: (optional) numerical hierarchy levels.

  • default_v{n}: (optional) standardized level codes.

  • leaf_id: (optional) unique integer for base/terminal cells.

  • contributing_leaf_ids: (optional) integer vectors for aggregation.

Examples

# Define hierarchies with some "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")

# Standard grid with all codes
hier_grid(h1, h2)
#>         v1     v2 cell_id leaf_id
#>     <char> <char>  <char>   <int>
#>  1:  Total  Total 0000000      NA
#>  2:      A  Total 0100000      NA
#>  3:     a1  Total 0110000      NA
#>  4:    aa1  Total 0111000      NA
#>  5:      B  Total 0200000      NA
#>  6:      C  Total 0300000      NA
#>  7:  Total      a 0000010      NA
#>  8:      A      a 0100010      NA
#>  9:     a1      a 0110010      NA
#> 10:    aa1      a 0111010       3
#> 11:      B      a 0200010       1
#> 12:      C      a 0300010       2
#> 13:  Total      b 0000020      NA
#> 14:      A      b 0100020      NA
#> 15:     a1      b 0110020      NA
#> 16:    aa1      b 0111020      NA
#> 17:      B      b 0200020      NA
#> 18:      C      b 0300020      NA
#> 19:  Total     b1 0000021      NA
#> 20:      A     b1 0100021      NA
#> 21:     a1     b1 0110021      NA
#> 22:    aa1     b1 0111021       6
#> 23:      B     b1 0200021       4
#> 24:      C     b1 0300021       5
#> 25:  Total      c 0000030      NA
#> 26:      A      c 0100030      NA
#> 27:     a1      c 0110030      NA
#> 28:    aa1      c 0111030       9
#> 29:      B      c 0200030       7
#> 30:      C      c 0300030       8
#> 31:  Total      d 0000040      NA
#> 32:      A      d 0100040      NA
#> 33:     a1      d 0110040      NA
#> 34:    aa1      d 0111040      NA
#> 35:      B      d 0200040      NA
#> 36:      C      d 0300040      NA
#> 37:  Total     d1 0000041      NA
#> 38:      A     d1 0100041      NA
#> 39:     a1     d1 0110041      NA
#> 40:    aa1     d1 0111041      12
#> 41:      B     d1 0200041      10
#> 42:      C     d1 0300041      11
#> 43:  Total      e 0000050      NA
#> 44:      A      e 0100050      NA
#> 45:     a1      e 0110050      NA
#> 46:    aa1      e 0111050      15
#> 47:      B      e 0200050      13
#> 48:      C      e 0300050      14
#>         v1     v2 cell_id leaf_id
#>     <char> <char>  <char>   <int>

# Grid without bogus codes
# h1: `A` and `a1` are replaced by `aa1`
# h2: `b` and `d` are replaced by `b1` and `d1`
# This ensures the grid is consistent with the most granular data.
hier_grid(h1, h2, add_dups = FALSE)
#>         v1     v2 cell_id leaf_id
#>     <char> <char>  <char>   <int>
#>  1:  Total  Total 0000000      NA
#>  2:    aa1  Total 0111000      NA
#>  3:      B  Total 0200000      NA
#>  4:      C  Total 0300000      NA
#>  5:  Total      a 0000010      NA
#>  6:    aa1      a 0111010       3
#>  7:      B      a 0200010       1
#>  8:      C      a 0300010       2
#>  9:  Total     b1 0000021      NA
#> 10:    aa1     b1 0111021       6
#> 11:      B     b1 0200021       4
#> 12:      C     b1 0300021       5
#> 13:  Total      c 0000030      NA
#> 14:    aa1      c 0111030       9
#> 15:      B      c 0200030       7
#> 16:      C      c 0300030       8
#> 17:  Total     d1 0000041      NA
#> 18:    aa1     d1 0111041      12
#> 19:      B     d1 0200041      10
#> 20:      C     d1 0300041      11
#> 21:  Total      e 0000050      NA
#> 22:    aa1      e 0111050      15
#> 23:      B      e 0200050      13
#> 24:      C      e 0300050      14
#>         v1     v2 cell_id leaf_id
#>     <char> <char>  <char>   <int>

# Advanced grid with contributing indices
# The 'cell_id' will be a concatenated string like '0000000'
# The 'contributing_leaf_ids' column allows for high-speed aggregation
# and/or matching with micro-data (for an example see `?hier_create_ids()`)
hier_grid(h1, h2,
  add_dups = FALSE,
  add_contributing_cells = TRUE
)
#>         v1     v2 cell_id leaf_id contributing_leaf_ids
#>     <char> <char>  <char>   <int>                <list>
#>  1:  Total  Total 0000000      NA   1,2,3,7,8,9,...[15]
#>  2:    aa1  Total 0111000      NA         3, 9,15, 6,12
#>  3:      B  Total 0200000      NA         1, 7,13, 4,10
#>  4:      C  Total 0300000      NA         2, 8,14, 5,11
#>  5:  Total      a 0000010      NA                 1,2,3
#>  6:    aa1      a 0111010       3                     3
#>  7:      B      a 0200010       1                     1
#>  8:      C      a 0300010       2                     2
#>  9:  Total     b1 0000021      NA                 4,5,6
#> 10:    aa1     b1 0111021       6                     6
#> 11:      B     b1 0200021       4                     4
#> 12:      C     b1 0300021       5                     5
#> 13:  Total      c 0000030      NA                 7,8,9
#> 14:    aa1      c 0111030       9                     9
#> 15:      B      c 0200030       7                     7
#> 16:      C      c 0300030       8                     8
#> 17:  Total     d1 0000041      NA              10,11,12
#> 18:    aa1     d1 0111041      12                    12
#> 19:      B     d1 0200041      10                    10
#> 20:      C     d1 0300041      11                    11
#> 21:  Total      e 0000050      NA              13,14,15
#> 22:    aa1      e 0111050      15                    15
#> 23:      B      e 0200050      13                    13
#> 24:      C      e 0300050      14                    14
#>         v1     v2 cell_id leaf_id contributing_leaf_ids
#>     <char> <char>  <char>   <int>                <list>