Learn R Programming

SSBtools (version 1.7.0)

hierarchies_as_vars: Hierarchies coded as variables

Description

The hierarchical relations are stored as minimal datasets

Usage

hierarchies_as_vars(
  hierarchies,
  name_function = function(name, level) paste0(name, "_level_", level),
  single_vars = FALSE,
  from_dummy = NA,
  dummy_reorder = TRUE,
  combine_vars = TRUE,
  drop_codes = NULL,
  include_codes = NULL,
  ...
)

Value

Named list of data frames

Arguments

hierarchies

List of hierarchies in the same format as input to AutoHierarchies

name_function

A function defining how to name all columns except the first. The input consists of the hierarchy name (identical to the first column’s name, name) and the column number minus 1 (level).

single_vars

When TRUE, a single variable is created for all codes except the input codes.

from_dummy

Logical value indicating the method for handling hierarchies.

  • When TRUE, the algorithm uses dummy-coded hierarchies.

  • When FALSE, the algorithm works directly on hierarchies standardized by AutoHierarchies, often resulting in well-structured output variables.

  • When NA (default), the algorithm first attempts the FALSE method; if not feasible, it falls back to the TRUE method.

dummy_reorder

When TRUE, dummy-coded hierarchies are reordered to potentially improve the structure of output variables.

combine_vars

When TRUE, an algorithm is applied to potentially reduce the number of output variables needed.

drop_codes

A named list of codes (except the input codes) to be dropped from the output. The list must have the same names as the hierarchies, but not all names/elements need to be included.

include_codes

Similar to drop_codes, but specifies the codes to be included instead.

...

Additional parameters passed to AutoHierarchies

See Also

vars_to_hierarchies

Examples

Run this code
# Examples based on those from AutoHierarchies
# You may also try converting other examples from AutoHierarchies

z <- SSBtoolsData("sprt_emp_withEU")
year_formula <- c("y_14 = 2014", "y_15_16 = y_all - y_14", "y_all = 2014 + 2015 + 2016")
geo_dim_list <- FindDimLists(z[, c("geo", "eu")], total = "Europe")[[1]]
age_hierarchy <- SSBtoolsData("sprt_emp_ageHier")

hierarchies_as_vars(list(age = age_hierarchy, geo = geo_dim_list, year = year_formula))
hierarchies_as_vars(list(age = age_hierarchy, geo = geo_dim_list, year = year_formula), 
                    singleVars = TRUE)
                    
# NAs are included in data when necessary
hierarchies_as_vars(list(f = c("AB = A + B", "AC = A + C", "CD = C + D", "ABCD = AB + CD")))                     

# drop_codes and include_codes  
hierarchies_as_vars(list(age = age_hierarchy, geo = geo_dim_list, year = year_formula), 
                    drop_codes = list(geo = "nonEU", year = c("y_14", "y_all")))  
hierarchies_as_vars(list(age = age_hierarchy, geo = geo_dim_list, year = year_formula), 
                    include_codes = list(year = c("y_14", "y_all")))      
               

Run the code above in your browser using DataLab