Learn R Programming

SSBtools (version 1.7.0)

map_hierarchies_to_data: Add variables to dataset based on hierarchies

Description

Uses hierarchies_as_vars to transform hierarchies, followed by mapping to the dataset.

Usage

map_hierarchies_to_data(
  data,
  hierarchies,
  when_overwritten = warning,
  add_comment = TRUE,
  ...
)

Value

Input data with extra Variables

Arguments

data

A data frame containing variables with names matching the names of the hierarchies.

hierarchies

List of hierarchies in the same format as input to AutoHierarchies

when_overwritten

A function to be called when existing column(s) are overwritten. Supply stop to invoke an error, warning for a warning (default), message to display an informational message, or NULL to do nothing.

add_comment

Logical. When TRUE (default), a comment attribute will be added to the output data frame, containing the names of the variables that were added.

...

Further parameters sent to hierarchies_as_vars

Examples

Run this code

# Examples similar those from hierarchies_as_vars

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")

map_hierarchies_to_data(z, list(age = age_hierarchy, geo = geo_dim_list, 
                                year = year_formula))

map_hierarchies_to_data(data.frame(f = c("A", "B", "C", "D", "E", "A")), list(f = 
       c("AB = A + B", "AC = A + C", "CD = C + D", "ABCD = AB + CD")))
       
       
# Examples demonstrating when_overwritten and add_comment        
       
a <- map_hierarchies_to_data(z, list(age = age_hierarchy, geo = geo_dim_list))
comment(a)

b <- map_hierarchies_to_data(a[-7], list(age = age_hierarchy, geo = geo_dim_list), 
                             when_overwritten = message, add_comment = FALSE)
comment(b)

Run the code above in your browser using DataLab