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.
hier_grid(..., add_dups = TRUE, add_levs = FALSE, add_default_codes = FALSE)one or more hierarchy objects created with hier_create() or
hier_compute()
scalar logical defining if bogus codes (codes that are the only leaf contributing to a parent that also has no siblings) should be included.
scalar logical defining if numerical levels for each codes should
be appended to the output data.table.
scalar logical definining if standardized level codes should be additionally returned
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
# 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)
#> v1 v2
#> <char> <char>
#> 1: Total Total
#> 2: A Total
#> 3: a1 Total
#> 4: aa1 Total
#> 5: B Total
#> 6: C Total
#> 7: Total a
#> 8: A a
#> 9: a1 a
#> 10: aa1 a
#> 11: B a
#> 12: C a
#> 13: Total b
#> 14: A b
#> 15: a1 b
#> 16: aa1 b
#> 17: B b
#> 18: C b
#> 19: Total b1
#> 20: A b1
#> 21: a1 b1
#> 22: aa1 b1
#> 23: B b1
#> 24: C b1
#> 25: Total c
#> 26: A c
#> 27: a1 c
#> 28: aa1 c
#> 29: B c
#> 30: C c
#> 31: Total d
#> 32: A d
#> 33: a1 d
#> 34: aa1 d
#> 35: B d
#> 36: C d
#> 37: Total d1
#> 38: A d1
#> 39: a1 d1
#> 40: aa1 d1
#> 41: B d1
#> 42: C d1
#> 43: Total e
#> 44: A e
#> 45: a1 e
#> 46: aa1 e
#> 47: B e
#> 48: C e
#> v1 v2
# only the required codes to build the complete hierarchy (no bogus codes)
hier_grid(h1, h2, add_dups = FALSE)
#> v1 v2
#> <char> <char>
#> 1: Total Total
#> 2: A Total
#> 3: B Total
#> 4: C Total
#> 5: Total a
#> 6: A a
#> 7: B a
#> 8: C a
#> 9: Total b
#> 10: A b
#> 11: B b
#> 12: C b
#> 13: Total c
#> 14: A c
#> 15: B c
#> 16: C c
#> 17: Total d
#> 18: A d
#> 19: B d
#> 20: C d
#> 21: Total e
#> 22: A e
#> 23: B e
#> 24: C e
#> v1 v2
# also contain columns specifying the hierarchy level
hier_grid(h1, h2, add_dups = FALSE, add_levs = TRUE)
#> v1 v2 levs_v1 levs_v2
#> <char> <char> <int> <int>
#> 1: Total Total 1 1
#> 2: A Total 2 1
#> 3: B Total 2 1
#> 4: C Total 2 1
#> 5: Total a 1 2
#> 6: A a 2 2
#> 7: B a 2 2
#> 8: C a 2 2
#> 9: Total b 1 2
#> 10: A b 2 2
#> 11: B b 2 2
#> 12: C b 2 2
#> 13: Total c 1 2
#> 14: A c 2 2
#> 15: B c 2 2
#> 16: C c 2 2
#> 17: Total d 1 2
#> 18: A d 2 2
#> 19: B d 2 2
#> 20: C d 2 2
#> 21: Total e 1 2
#> 22: A e 2 2
#> 23: B e 2 2
#> 24: C e 2 2
#> v1 v2 levs_v1 levs_v2