from_4_to_3_case_0_hr: Transition from 4 to 3 variables by merging two...

View source: R/sp_from_4_to_3_case_0_hr.R

from_4_to_3_case_0_hrR Documentation

Transition from 4 to 3 variables by merging two non-hierarchical variables

Description

Transition from 4 to 3 variables by merging two non-hierarchical variables

Usage

from_4_to_3_case_0_hr(dfs, dfs_name, v1, v2, totcode, dir_name, sep = "_")

Arguments

dfs

data.frame with 4 categorical variables (n >= 2 in the general case)

dfs_name

name of the data.frame in the list provided by the user

v1

non-hierarchical categorical variable

v2

non-hierarchical categorical variable

totcode

named vector of totals for categorical variables

dir_name

folder where to write the hrc files if no folder is specified in hrcfiles

sep

separator used when concatenating variables

Value

A list containing:

  • tabs: named list of 3-dimensional dataframes (n-1 dimensions in the general case) with nested hierarchies

  • hrc: named list of hrc specific to the variable created via merging

  • alt_tot: named list of totals

  • vars: named list of vectors representing the merged variables during the two steps of dimension reduction

Examples

library(dplyr)
data <- expand.grid(
  ACT = c("Total", "A", "B", "A1", "A2", "B1", "B2"),
  SEX = c("Total", "F", "M","F1","F2","M1","M2"),
  AGE = c("Total", "AGE1", "AGE2", "AGE11", "AGE12", "AGE21", "AGE22"),
  ECO = c("PIB","Households","Companies"),
  stringsAsFactors = FALSE,
  KEEP.OUT.ATTRS = FALSE
) %>%
  as.data.frame()

data <- data %>% mutate(VALUE = 1:n())

hrc_act <- "hrc_ACT.hrc"
sdcHierarchies::hier_create(root = "Total", nodes = c("A","B")) %>%
  sdcHierarchies::hier_add(root = "A", nodes = c("A1","A2")) %>%
  sdcHierarchies::hier_convert(as = "argus") %>%
  slice(-1) %>%
  mutate(levels = substring(paste0(level,name),3)) %>%
  select(levels) %>%
  write.table(file = hrc_act, row.names = FALSE, col.names = FALSE, quote = FALSE)

hrc_sex <- "hrc_SEX.hrc"
sdcHierarchies::hier_create(root = "Total", nodes = c("F","M")) %>%
  sdcHierarchies::hier_add(root = "F", nodes = c("F1","F2")) %>%
  sdcHierarchies::hier_add(root = "M", nodes = c("M1","M2")) %>%
  sdcHierarchies::hier_convert(as = "argus") %>%
  slice(-1) %>%
  mutate(levels = substring(paste0(level,name),3)) %>%
  select(levels) %>%
  write.table(file = hrc_sex, row.names = FALSE, col.names = FALSE, quote = FALSE)

res1 <- from_4_to_3_case_0_hr(dfs = data,
                                dfs_name = "dfs_name",
                                v1 = "ECO",v2 = "AGE",
                                totcode = c(ACT = "Total",SEX = "Total",
                                            AGE = "Total",ECO = "PIB"),
                                dir_name = "output")

InseeFrLab/rtauargus documentation built on Feb. 25, 2025, 6:32 a.m.