length_tabs: Calculation of the table sizes generated a priori during the...

View source: R/sp_var_to_merge.R

length_tabsR Documentation

Calculation of the table sizes generated a priori during the reduction of dimension from 4 or 5 dimensions to 3 dimensions

Description

Calculation of the table sizes generated a priori during the reduction of dimension from 4 or 5 dimensions to 3 dimensions

Usage

length_tabs(dfs, v1, v2, v3 = NULL, v4 = NULL, totcode, hrcfiles = NULL)

Arguments

dfs

a data.frame

Variable in the 5->4 or 4->3 step

v1

the first merged variable

v2

the second merged variable

Variable in the case of 4->3 passage in the 4->3 process do not specify v1_v2 if three variables are merged into one

v3

the third original variable to be merged

v4

the fourth original variable to be merged

totcode

character named vector

hrcfiles

named vector of hrc files related to the variables

Value

a list of the lengths of the tables created during the dimension reduction

Examples

# Dimension 4
library(dplyr)
data <- expand.grid(
  ACT = c("Total", "A", "B", "A1", "A2","A3", "B1", "B2","B3","B4","C",
          "name_non_changed_vars","E","F","G","B5"),
  GEO = c("Total", "G1", "G2"),
  SEX = c("Total", "F", "M"),
  AGE = c("Total", "AGE1", "AGE2"),
  stringsAsFactors = FALSE
) %>%
  as.data.frame()

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


hrc_act <- "hrc_ACT.hrc"

sdcHierarchies::hier_create(
  root = "Total",
  nodes = c("A","B","C","name_non_changed_vars","E","F","G")
) %>%
  sdcHierarchies::hier_add(root = "A", nodes = c("A1","A2","A3")) %>%
  sdcHierarchies::hier_add(root = "B", nodes = c("B1","B2","B3","B4","B5")) %>%
  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)

# Function results

res1 <- length_tabs(dfs = data,
                    hrcfiles = c(ACT = hrc_act),
                    totcode = c(SEX="Total",AGE="Total", GEO="Total", ACT="Total"),
                    v1 = "ACT",
                    v2 = "GEO")

# Dimension 5
data <- expand.grid(
  ACT = c("Total_A", paste0("A", seq(1,5),"_"),
          paste0("A1_", seq(1,7)),paste0("A2_", seq(1,9))),
  GEO = c("Total_G", "GA", "GB", "GA1", "GA2", "GB1", "GB2","GA3","GB3","GB4"),
  SEX = c("Total_S", "F", "M","F1","F2","M1","M2"),
  AGE = c("Ensemble", "AGE1", "AGE2", "AGE11", "AGE12", "AGE21", "AGE22"),
  ECO = c("PIB","Ménages","Entreprises"),
  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_A", nodes = paste0("A", seq(1,5),"_")) %>%
  sdcHierarchies::hier_add(root = "A1_", nodes = paste0("A1_", seq(1,7))) %>%
  sdcHierarchies::hier_add(root = "A2_", nodes = paste0("A2_", seq(1,9))) %>%
  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_geo <- "hrc_GEO.hrc"
sdcHierarchies::hier_create(root = "Total_G", nodes = c("GA","GB")) %>%
  sdcHierarchies::hier_add(root = "GA", nodes = c("GA1","GA2","GA3")) %>%
  sdcHierarchies::hier_add(root = "GB", nodes = c("GB1","GB2","GB3","GB4")) %>%
  sdcHierarchies::hier_convert(as = "argus") %>%
  slice(-1) %>%
  mutate(levels = substring(paste0(level,name),3)) %>%
  select(levels) %>%
  write.table(file = hrc_geo, row.names = FALSE, col.names = FALSE, quote = FALSE)

res2 <- length_tabs(dfs = data,
                    hrcfiles = c(ACT = hrc_act, GEO = hrc_geo),
                    totcode = c(SEX="Total_S",AGE="Ensemble", GEO="Total_G",
                                ACT="Total_A", ECO = "PIB"),
                    v1 = "ACT",v2 = "AGE",
                    v3 = "GEO",v4 = "SEX")

res3 <- length_tabs(dfs = data,
                    hrcfiles = c(ACT = hrc_act, GEO = hrc_geo),
                    totcode = c(SEX="Total_S",AGE="Ensemble", GEO="Total_G",
                                ACT="Total_A", ECO = "PIB"),
                    v1 = "ACT",v2 = "AGE",v3 = "GEO")

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