| Title: | Create and (Interactively) Modify Nested Hierarchies |
|---|---|
| Description: | Provides functionality to generate, (interactively) modify (by adding, removing and renaming nodes) and convert nested hierarchies between different formats. These tree like structures can be used to define for example complex hierarchical tables used for statistical disclosure control. |
| Authors: | Bernhard Meindl [aut, cre] |
| Maintainer: | Bernhard Meindl <[email protected]> |
| License: | GPL-3 |
| Version: | 0.23.1 |
| Built: | 2026-05-12 14:23:23 UTC |
| Source: | https://github.com/bernhard-da/sdchierarchies |
This function allows to add nodes (levels) to an existing nested hierarchy.
hier_add(tree, root, nodes)hier_add(tree, root, nodes)
tree |
a (nested) hierarchy created using |
root |
(character) a name of an existing node in the hierarchy |
nodes |
(character) names of new nodes that should be added below
|
h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) hier_display(h)h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) hier_display(h)
This function starts the interactive shiny-app
to (optionally) create and/or modify a nested hierarchy.
It is possible to supply a character vector from which the
hierarchy can be interactively built. Once this has been done,
it is possible to modify and export the resulting hierarchy.
hier_app(x = hier_create(), ...)hier_app(x = hier_create(), ...)
x |
a character vector containing nested levels or
an object generated with |
... |
arguments (e.g |
Another option is to supply an already existing hierarchy object. In this case, it is possible to modify the existing object in the app.
The app can return a hierarchy object (either
a data.frame or a tree-based object)
## Not run: # start with an empty hierarchy res <- hier_app() # start with a character vector that is used to build the hierarchy codes <- c("11", "12", "21", "22", "23", "31", "32") res <- hier_app(codes); print(res) ## End(Not run)## Not run: # start with an empty hierarchy res <- hier_app() # start with a character vector that is used to build the hierarchy codes <- c("11", "12", "21", "22", "23", "31", "32") res <- hier_app(codes); print(res) ## End(Not run)
hier_codes() returns the standardized codes for the nodes of a tree.
hier_codes(tree)hier_codes(tree)
tree |
a (nested) hierarchy created using |
a named character vector with names being the node-names and the values the standardized codes
h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) hier_codes(h)h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) hier_codes(h)
This function allows to compute a nested hierarchy from an character vector or a (named) list.
hier_compute(inp, dim_spec = NULL, root = NULL, method = "len", as = "network")hier_compute(inp, dim_spec = NULL, root = NULL, method = "len", as = "network")
inp |
a character vector (for methods |
dim_spec |
an (integerish) vector containing either the length
(in terms of characters) for each level or the end-positions of
these levels. In the latter-case, one needs to set argument
|
root |
|
method |
either
|
as |
(character) specifies the type of the return object. Possible choices are:
|
a hierarchical data structure depending on choice of
argument as
## Example Regional Codes (NUTS) # digits 1-2 (len=2, endpos=2) --> level 1 # digit 3 (len=1, endpos=3) --> level 2 # digits 4-5 (len=2, endpos=5) -> level 3 # all strings have equal length but total is not encoded in these values geo_m <- c( "01051", "01053", "01054", "01055", "01056", "01057", "01058", "01059", "01060", "01061", "01062", "02000", "03151", "03152", "03153", "03154", "03155", "03156", "03157", "03158", "03251", "03252", "03254", "03255", "03256", "03257", "03351", "03352", "03353", "03354", "03355", "03356", "03357", "03358", "03359", "03360", "03361", "03451", "03452", "03453", "03454", "03455", "03456", "10155") a <- hier_compute( inp = geo_m, dim_spec = c(2, 3, 5), root = "Tot", method = "endpos" ) b <- hier_compute( inp = geo_m, dim_spec = c(2, 1, 2), root = "Tot", method = "len" ) identical( hier_convert(a, as = "df"), hier_convert(b, as = "df") ) # total is contained in the first 3 positions of the input values # --> we need to set name of the overall total (argument "root") # to NULL (the default) geo_m_with_tot <- paste0("Tot", geo_m) a <- hier_compute( inp = geo_m_with_tot, dim_spec = c(3, 2, 1, 2), method = "len" ) b <- hier_compute( inp = geo_m_with_tot, dim_spec = c(3, 5, 6, 8), method = "endpos" ) identical(a, b) # example where inputs have unequal length # the overall total is not included in input vector yae_h <- c( "1.1.1.", "1.1.2.", "1.2.1.", "1.2.2.", "1.2.3.", "1.2.4.", "1.2.5.", "1.3.1.", "1.3.2.", "1.3.3.", "1.3.4.", "1.3.5.", "1.4.1.", "1.4.2.", "1.4.3.", "1.4.4.", "1.4.5.", "1.5.", "1.6.", "1.7.", "1.8.", "1.9.", "2.", "3.") a <- hier_compute( inp = yae_h, dim_spec = c(2, 4, 6), root = "Tot", method = "endpos" ) b <- hier_compute( inp = yae_h, dim_spec = c(2, 2, 2), root = "Tot", method = "len" ) identical( hier_convert(a, as = "df"), hier_convert(b, as = "df") ) # Same example, but overall total is contained in the first 3 positions # of the input values --> argument "root" needs to be # set to NULL (the default) yae_h_with_tot <- paste0("Tot", yae_h) a <- hier_compute( inp = yae_h_with_tot, dim_spec = c(3, 2, 2, 2), method = "len", ) b <- hier_compute( inp = yae_h_with_tot, dim_spec = c(3, 5, 7, 9), method = "endpos" ) identical(a, b) # An example using a list as input (same as above) # Hierarchy: digits 1-2 (nuts1), digit 3 (nut2), digits 4-5 (nuts3) # The order of the list-elements is not important but the # names of input-list correspond to (subtotal/level) names geo_ll <- list() geo_ll[["Total"]] <- c("01", "02", "03", "10") geo_ll[["010"]] <- c( "01051", "01053", "01054", "01055", "01056", "01057", "01058", "01059", "01060", "01061", "01062" ) geo_ll[["031"]] <- c( "03151", "03152", "03153", "03154", "03155", "03156", "03157", "03158" ) geo_ll[["032"]] <- c( "03251", "03252", "03254", "03255", "03256", "03257" ) geo_ll[["033"]] <- c( "03351", "03352", "03353", "03354", "03355", "03356", "03357", "03358", "03359", "03360", "03361" ) geo_ll[["034"]] <- c( "03451", "03452", "03453", "03454", "03455","03456" ) geo_ll[["01"]] <- "010" geo_ll[["02"]] <- "020" geo_ll[["020"]] <- "02000" geo_ll[["03"]] <- c("031", "032", "033", "034") geo_ll[["10"]] <- "101" geo_ll[["101"]] <- "10155" d <- hier_compute( inp = geo_ll, root = "Total", method = "list" ); d ## Reproduce example from above with input defined as named list yae_ll <- list() yae_ll[["Total"]] <- c("1.", "2.", "3.") yae_ll[["1."]] <- paste0("1.", 1:9, ".") yae_ll[["1.1."]] <- paste0("1.1.", 1:2, ".") yae_ll[["1.2."]] <- paste0("1.2.", 1:5, ".") yae_ll[["1.3."]] <- paste0("1.3.", 1:5, ".") yae_ll[["1.4."]] <- paste0("1.4.", 1:6, ".") # return result as data.frame d <- hier_compute( inp = yae_ll, root = "Total", method = "list", as = "df" ); d## Example Regional Codes (NUTS) # digits 1-2 (len=2, endpos=2) --> level 1 # digit 3 (len=1, endpos=3) --> level 2 # digits 4-5 (len=2, endpos=5) -> level 3 # all strings have equal length but total is not encoded in these values geo_m <- c( "01051", "01053", "01054", "01055", "01056", "01057", "01058", "01059", "01060", "01061", "01062", "02000", "03151", "03152", "03153", "03154", "03155", "03156", "03157", "03158", "03251", "03252", "03254", "03255", "03256", "03257", "03351", "03352", "03353", "03354", "03355", "03356", "03357", "03358", "03359", "03360", "03361", "03451", "03452", "03453", "03454", "03455", "03456", "10155") a <- hier_compute( inp = geo_m, dim_spec = c(2, 3, 5), root = "Tot", method = "endpos" ) b <- hier_compute( inp = geo_m, dim_spec = c(2, 1, 2), root = "Tot", method = "len" ) identical( hier_convert(a, as = "df"), hier_convert(b, as = "df") ) # total is contained in the first 3 positions of the input values # --> we need to set name of the overall total (argument "root") # to NULL (the default) geo_m_with_tot <- paste0("Tot", geo_m) a <- hier_compute( inp = geo_m_with_tot, dim_spec = c(3, 2, 1, 2), method = "len" ) b <- hier_compute( inp = geo_m_with_tot, dim_spec = c(3, 5, 6, 8), method = "endpos" ) identical(a, b) # example where inputs have unequal length # the overall total is not included in input vector yae_h <- c( "1.1.1.", "1.1.2.", "1.2.1.", "1.2.2.", "1.2.3.", "1.2.4.", "1.2.5.", "1.3.1.", "1.3.2.", "1.3.3.", "1.3.4.", "1.3.5.", "1.4.1.", "1.4.2.", "1.4.3.", "1.4.4.", "1.4.5.", "1.5.", "1.6.", "1.7.", "1.8.", "1.9.", "2.", "3.") a <- hier_compute( inp = yae_h, dim_spec = c(2, 4, 6), root = "Tot", method = "endpos" ) b <- hier_compute( inp = yae_h, dim_spec = c(2, 2, 2), root = "Tot", method = "len" ) identical( hier_convert(a, as = "df"), hier_convert(b, as = "df") ) # Same example, but overall total is contained in the first 3 positions # of the input values --> argument "root" needs to be # set to NULL (the default) yae_h_with_tot <- paste0("Tot", yae_h) a <- hier_compute( inp = yae_h_with_tot, dim_spec = c(3, 2, 2, 2), method = "len", ) b <- hier_compute( inp = yae_h_with_tot, dim_spec = c(3, 5, 7, 9), method = "endpos" ) identical(a, b) # An example using a list as input (same as above) # Hierarchy: digits 1-2 (nuts1), digit 3 (nut2), digits 4-5 (nuts3) # The order of the list-elements is not important but the # names of input-list correspond to (subtotal/level) names geo_ll <- list() geo_ll[["Total"]] <- c("01", "02", "03", "10") geo_ll[["010"]] <- c( "01051", "01053", "01054", "01055", "01056", "01057", "01058", "01059", "01060", "01061", "01062" ) geo_ll[["031"]] <- c( "03151", "03152", "03153", "03154", "03155", "03156", "03157", "03158" ) geo_ll[["032"]] <- c( "03251", "03252", "03254", "03255", "03256", "03257" ) geo_ll[["033"]] <- c( "03351", "03352", "03353", "03354", "03355", "03356", "03357", "03358", "03359", "03360", "03361" ) geo_ll[["034"]] <- c( "03451", "03452", "03453", "03454", "03455","03456" ) geo_ll[["01"]] <- "010" geo_ll[["02"]] <- "020" geo_ll[["020"]] <- "02000" geo_ll[["03"]] <- c("031", "032", "033", "034") geo_ll[["10"]] <- "101" geo_ll[["101"]] <- "10155" d <- hier_compute( inp = geo_ll, root = "Total", method = "list" ); d ## Reproduce example from above with input defined as named list yae_ll <- list() yae_ll[["Total"]] <- c("1.", "2.", "3.") yae_ll[["1."]] <- paste0("1.", 1:9, ".") yae_ll[["1.1."]] <- paste0("1.1.", 1:2, ".") yae_ll[["1.2."]] <- paste0("1.2.", 1:5, ".") yae_ll[["1.3."]] <- paste0("1.3.", 1:5, ".") yae_ll[["1.4."]] <- paste0("1.4.", 1:6, ".") # return result as data.frame d <- hier_compute( inp = yae_ll, root = "Total", method = "list", as = "df" ); d
This functions allows to convert nested hierarchies into other data structures.
hier_convert(tree, as = "df")hier_convert(tree, as = "df")
tree |
a (nested) hierarchy created using |
as |
(character) specifying the export format. Possible choices are:
|
h <- hier_create(root = "Total", nodes = LETTERS[1:2]) h <- hier_add(h, root = "A", nodes = c("a1", "a2")) h <- hier_add(h, root = "B", nodes = c("b1", "b2")) h <- hier_add(h, root = "b1", nodes = "b1a") hier_display(h) # required code to build the hierarchy hier_convert(h, as = "code") # data.frame hier_convert(h, as = "df")h <- hier_create(root = "Total", nodes = LETTERS[1:2]) h <- hier_add(h, root = "A", nodes = c("a1", "a2")) h <- hier_add(h, root = "B", nodes = c("b1", "b2")) h <- hier_add(h, root = "b1", nodes = "b1a") hier_display(h) # required code to build the hierarchy hier_convert(h, as = "code") # data.frame hier_convert(h, as = "df")
This functions allows to generate a hierarchical
data structure that can be used in other packages such
as cellKey
or sdcTable.
hier_create(root = "Total", nodes = NULL)hier_create(root = "Total", nodes = NULL)
root |
(character) name of the overall total |
nodes |
(character) name of leaves (nodes) in the hierarchy |
a (nested) sdc hierarchy tree
hier_add hier_delete hier_rename hier_export hier_convert hier_app hier_info
# without nodes h <- hier_create(root = "tot") hier_display(h) # with nodes h <- hier_create(root = "tot", nodes = LETTERS[1:5]) hier_display(h)# without nodes h <- hier_create(root = "tot") hier_display(h) # with nodes h <- hier_create(root = "tot", nodes = LETTERS[1:5]) hier_display(h)
This function calculates linearized integer IDs for each record in a micro dataset based on a set of hierarchies. These IDs match the 'leaf_id' and 'contributing_leaf_ids' generated by [hier_grid()].
hier_create_ids(data, dims)hier_create_ids(data, dims)
data |
a 'data.table' containing the microdata. |
dims |
a named 'list' of 'sdc_hierarchy' objects. The names of the list elements must correspond to existing column names in 'data'. |
an integer vector of leaf_ids matching the 'leaf_id' column in [hier_grid()]
# Setup Hierarchies h1 <- hier_create("Total", nodes = LETTERS[1:3]) h1 <- hier_add(h1, root = "A", node = "a1") h1 <- hier_add(h1, root = "a1", node = "aa1") # h1 terminals: aa1, B, C (N=3) h2 <- hier_create("Total", letters[1:2]) # h2 terminals: a, b (N=2) # Create the Grid # With add_dups = FALSE, bogus parents 'A' and 'a1' are removed. grid <- hier_grid(h1, h2, add_dups = FALSE, add_contributing_cells = TRUE) # The 'leaf_id' in `grid` is calculated using Column-Major order: # ID = i1 + (i2 - 1) * N1 # Generate micro data microdata <- data.table::data.table( region = c("aa1", "aa1", "B", "C", "B"), sector = c("a", "b", "a", "b", "b"), turnover = c(100, 200, 50, 300, 150) ) # Map the strings in microdata to the same integer leaf_ids. # We provide a named list where names 'region' and 'sector' match microdata. microdata[, leaf_id := hier_create_ids( data = microdata, dims = list(region = h1, sector = h2) )] # Aggregation Example: # To get 'Total Region' for 'Sector a' from the grid: target_cell <- grid[v1 == "Total" & v2 == "a"] ids <- target_cell$contributing_leaf_ids[[1]] val_total_a <- sum(microdata[leaf_id %in% ids, turnover]) # Result: 150 (Records: aa1_a [100] + B_a [50])# Setup Hierarchies h1 <- hier_create("Total", nodes = LETTERS[1:3]) h1 <- hier_add(h1, root = "A", node = "a1") h1 <- hier_add(h1, root = "a1", node = "aa1") # h1 terminals: aa1, B, C (N=3) h2 <- hier_create("Total", letters[1:2]) # h2 terminals: a, b (N=2) # Create the Grid # With add_dups = FALSE, bogus parents 'A' and 'a1' are removed. grid <- hier_grid(h1, h2, add_dups = FALSE, add_contributing_cells = TRUE) # The 'leaf_id' in `grid` is calculated using Column-Major order: # ID = i1 + (i2 - 1) * N1 # Generate micro data microdata <- data.table::data.table( region = c("aa1", "aa1", "B", "C", "B"), sector = c("a", "b", "a", "b", "b"), turnover = c(100, 200, 50, 300, 150) ) # Map the strings in microdata to the same integer leaf_ids. # We provide a named list where names 'region' and 'sector' match microdata. microdata[, leaf_id := hier_create_ids( data = microdata, dims = list(region = h1, sector = h2) )] # Aggregation Example: # To get 'Total Region' for 'Sector a' from the grid: target_cell <- grid[v1 == "Total" & v2 == "a"] ids <- target_cell$contributing_leaf_ids[[1]] val_total_a <- sum(microdata[leaf_id %in% ids, turnover]) # Result: 150 (Records: aa1_a [100] + B_a [50])
This function allows to delete nodes (levels) from an existing nested hierarchy.
hier_delete(tree, nodes)hier_delete(tree, nodes)
tree |
a (nested) hierarchy created using |
nodes |
character vector of nodes that should be deleted |
h <- hier_create(root = "Total", nodes = LETTERS[1:2]) h <- hier_add(h, root = "A", nodes = c("a1", "a2")) h <- hier_add(h, root = "B", nodes = c("b1", "b2")) h <- hier_add(h, root = "b1", nodes = "b1a") hier_display(h) h <- hier_delete(h, nodes = c("a1", "b1a")) hier_display(h)h <- hier_create(root = "Total", nodes = LETTERS[1:2]) h <- hier_add(h, root = "A", nodes = c("a1", "a2")) h <- hier_add(h, root = "B", nodes = c("b1", "b2")) h <- hier_add(h, root = "b1", nodes = "b1a") hier_display(h) h <- hier_delete(h, nodes = c("a1", "b1a")) hier_display(h)
This function shows the entire hierarchy in a nice way.
hier_display(x, root = NULL)hier_display(x, root = NULL)
x |
a hierarchy object, either directly generated and modified using
|
root |
|
NULL; the tree is printed to the prompt
h <- hier_create(root = "Total", nodes = LETTERS[1:2]) h <- hier_add(h, root = "A", nodes = c("a1", "a2")) # display the entire tree hier_display(h) # display only a subtree hier_display(h, root = "A")h <- hier_create(root = "Total", nodes = LETTERS[1:2]) h <- hier_add(h, root = "A", nodes = c("a1", "a2")) # display the entire tree hier_display(h) # display only a subtree hier_display(h, root = "A")
This function allows to write nested hierarchies into files on your disk.
hier_export(tree, as = "df", path, verbose = FALSE)hier_export(tree, as = "df", path, verbose = FALSE)
tree |
a (nested) hierarchy created using |
as |
(character) specifying the export format. Possible choices are:
|
path |
(character) relative or absolute path where results should be written to |
verbose |
(logical) additional results |
h <- hier_create(root = "Total", nodes = LETTERS[1:2]) h <- hier_add(h, root = "A", nodes = c("a1", "a2")) h <- hier_add(h, root = "B", nodes = c("b1", "b2")) h <- hier_add(h, root = "b1", nodes = "b1a") hier_display(h) # export as input for tauArgus hier_export(h, as = "argus", path = file.path(tempdir(), "h.hrc"))h <- hier_create(root = "Total", nodes = LETTERS[1:2]) h <- hier_add(h, root = "A", nodes = c("a1", "a2")) h <- hier_add(h, root = "B", nodes = c("b1", "b2")) h <- hier_add(h, root = "b1", nodes = "b1a") hier_display(h) # export as input for tauArgus hier_export(h, as = "argus", path = file.path(tempdir(), "h.hrc"))
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 )hier_grid( ..., add_dups = TRUE, add_levs = FALSE, add_default_codes = FALSE, add_contributing_cells = FALSE )
... |
one or more hierarchy objects created with |
add_dups |
scalar logical defining if bogus codes (parents with only one
child and no siblings) should be included. If |
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 |
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.
# 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) # 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) # 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 )# 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) # 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) # 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 )
This function creates a nested sdc hierarchy from various input structures.
hier_import(inp, from = "json", root = NULL, keep_order = FALSE)hier_import(inp, from = "json", root = NULL, keep_order = FALSE)
inp |
an object that should be imported. Argument |
from |
(character) from which format should be imported. Possible choices are:
|
root |
optional name of overall total |
keep_order |
if |
a (nested) hierarchy
h <- hier_create(root = "Total", nodes = LETTERS[1:2]) h <- hier_add(h, root = "A", nodes = c("a1", "a2")) h <- hier_add(h, root = "B", nodes = c("b1", "b2")) h <- hier_add(h, root = "b1", nodes = "b1a") hier_display(h) df <- hier_convert(h, as = "df") hier_display(df) h2 <- hier_import(df, from = "df") hier_display(h2) # check order df <- data.frame( level = c("@", "@@", "@@"), name = c("T", "m", "f") ) hier_display(hier_import(df, from = "df")) # automatically sorted (T, f, m) hier_display(hier_import(df, from = "df", keep_order = TRUE)) # original order (T, m, f)h <- hier_create(root = "Total", nodes = LETTERS[1:2]) h <- hier_add(h, root = "A", nodes = c("a1", "a2")) h <- hier_add(h, root = "B", nodes = c("b1", "b2")) h <- hier_add(h, root = "b1", nodes = "b1a") hier_display(h) df <- hier_convert(h, as = "df") hier_display(df) h2 <- hier_import(df, from = "df") hier_display(h2) # check order df <- data.frame( level = c("@", "@@", "@@"), name = c("T", "m", "f") ) hier_display(hier_import(df, from = "df")) # automatically sorted (T, f, m) hier_display(hier_import(df, from = "df", keep_order = TRUE)) # original order (T, m, f)
hier_info() computes various information about hierarchy codes
or the (nested) hierarchy.
hier_info(tree, nodes = NULL)hier_info(tree, nodes = NULL)
tree |
a (nested) hierarchy created using |
nodes |
(character) names of new nodes that should be added below
|
a list with information about the required nodes.
If nodes is NULL (the default), the information is
computed for all available nodes of the hierarchy. The following
properties are computed:
exists: (logical) does the node exist
name: (character) node name
is_rootnode: (logical) is the node the overall root of the tree?
level: (numeric) what is the level of the node
is_leaf: (logical) is the node a leaf?
siblings: (character) what are siblings of this node?
contributing_codes: (character) which codes are contributing
to this node? If none (it is a leaf), NA is returned
children: (character) the names of the children of the node.
If it has none (it is a leaf), NA is returned
is_bogus: (logical) is it a bogus code (i. e the only
children of a leaf?)
h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) hier_display(h) # about a specific node hier_info(h, nodes = "a1") # about all nodes hier_info(h)h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) hier_display(h) # about a specific node hier_info(h, nodes = "a1") # about all nodes hier_info(h)
This function returns a data.table that maps original
and default codes.
hier_match(tree, nodes = NULL, inputs = "orig")hier_match(tree, nodes = NULL, inputs = "orig")
tree |
an input derived from |
nodes |
|
inputs |
(character) specifies what kind of node names are
provided in argument
|
a data.table with the following columns:
"orig": the original node names
'"default": the standardized names
"is_bogus": TRUE if the code is a "bogus" (duplicated)
node.
h <- hier_create(root = "Tot", nodes = letters[1:5]) h <- hier_add(h, root = "a", nodes = "a0") h2 <- hier_convert(tree = h, as = "dt") hier_match(tree = h, nodes = c("a", "b"), inputs = "orig") hier_match(tree = h2, nodes = c("01", "02"), inputs = "default")h <- hier_create(root = "Tot", nodes = letters[1:5]) h <- hier_add(h, root = "a", nodes = "a0") h2 <- hier_convert(tree = h, as = "dt") hier_match(tree = h, nodes = c("a", "b"), inputs = "orig") hier_match(tree = h2, nodes = c("01", "02"), inputs = "default")
This function allows to extract the all the names of the nodes including all (sub)-nodes and leaves in the given hierarchy.
hier_nodenames(tree, root = NULL)hier_nodenames(tree, root = NULL)
tree |
a (nested) hierarchy created using |
root |
(character) name of start node from which all lower level-names should be returned |
h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) hier_nodenames(h)h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) hier_nodenames(h)
This function allows to rename one or more node(s) (levels) in an existing nested hierarchy.
hier_rename(tree, nodes)hier_rename(tree, nodes)
tree |
a (nested) hierarchy created using |
nodes |
(character) new names of nodes/levels that should be changed as a named vector: names refer to old, existing names, the values to the new labels |
h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) hier_display(h) h <- hier_rename(h, nodes = c("a1" = "x1", "A" = "X")) hier_display(h)h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) hier_display(h) h <- hier_rename(h, nodes = c("a1" = "x1", "A" = "X")) hier_display(h)
This function returns a tree in default format (as for
example created using hier_create()) for objects created using
hier_convert().
hier_to_tree(inp)hier_to_tree(inp)
inp |
a nested tree object created using |
a nested hierarchy with default format
h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) sdc <- hier_convert(h, as = "sdc") hier_display(h) hier_display(hier_to_tree(h)) hier_display(hier_to_tree(sdc))h <- hier_create(root = "Total", nodes = LETTERS[1:3]) h <- hier_add(h, root = "A", nodes = c("a1", "a5")) sdc <- hier_convert(h, as = "sdc") hier_display(h) hier_display(hier_to_tree(h)) hier_display(hier_to_tree(sdc))
This function opens the introductionary package vignette and opens it in a new browser tab/window.
hier_vignette()hier_vignette()
a browser windows/tab with showing the vignette
## Not run: hier_vignette() ## End(Not run)## Not run: hier_vignette() ## End(Not run)