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)

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.

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.

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)
#> v1 v2 #> 1: Total Total #> 2: A Total #> 3: B Total #> 4: C Total #> 5: a1 Total #> 6: aa1 Total #> 7: Total a #> 8: A a #> 9: B a #> 10: C a #> 11: a1 a #> 12: aa1 a #> 13: Total b #> 14: A b #> 15: B b #> 16: C b #> 17: a1 b #> 18: aa1 b #> 19: Total c #> 20: A c #> 21: B c #> 22: C c #> 23: a1 c #> 24: aa1 c #> 25: Total d #> 26: A d #> 27: B d #> 28: C d #> 29: a1 d #> 30: aa1 d #> 31: Total e #> 32: A e #> 33: B e #> 34: C e #> 35: a1 e #> 36: aa1 e #> 37: Total b1 #> 38: A b1 #> 39: B b1 #> 40: C b1 #> 41: a1 b1 #> 42: aa1 b1 #> 43: Total d1 #> 44: A d1 #> 45: B d1 #> 46: C d1 #> 47: a1 d1 #> 48: aa1 d1 #> v1 v2
# only the required codes to build the complete hierarchy (no bogus codes) hier_grid(h1, h2, add_dups = FALSE)
#> v1 v2 #> 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 #> 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