R/aggregateCorrespondenceTable.R
aggregateCorrespondenceTable.RdAggregates numeric values from a source classification A into a target classification B using a correspondence table AB. Each value in A is redistributed to one or more B codes according to weights in AB (if available), or split equally when no valid weights exist.
aggregateCorrespondenceTable(AB, A, B = NULL)A list with:
result: data.frame with aggregated values for each B code
(and any additional columns from B, if provided),
mapping: standardized A→B mapping used internally,
diagnostics: list with total input value, mapped value,
coverage ratio, and codes in A that could not be mapped.
This function is used when data expressed in one classification must be converted into another classification using an A→B correspondence table.
The workflow is:
Detect and standardize column names in AB, A and B
(case-insensitive).
For each code in A, determine all corresponding B codes.
Apply proportional allocation:
If AB contains a numeric weight column, weights
are normalized per source code.
If weights are missing, invalid, or sum to zero, values are split equally across all mapped B codes.
Values are multiplied by the resulting weights and aggregated by B code.
If B is provided, the result is aligned to its list of codes
(unmatched codes receive 0).
This function is appropriate for tasks such as converting statistical datasets from NACE to CPA, CPA to CN, CPC to HS, or any situation where values have to be re-expressed according to a different classification system.
retrieveCorrespondenceTable for downloading AB tables.
## Simple example (equal split)
AB <- data.frame(
from_code = c("A1","A1","A2"),
to_code = c("B1","B2","B1")
)
print(AB)
#> from_code to_code
#> 1 A1 B1
#> 2 A1 B2
#> 3 A2 B1
A_tbl <- data.frame(
code = c("A1","A2"),
value = c(100, 50)
)
print(A_tbl)
#> code value
#> 1 A1 100
#> 2 A2 50
result <- aggregateCorrespondenceTable(AB, A_tbl)
print(result$result)
#> code_B value
#> 1 B1 100
#> 2 B2 50
print(result$mapping)
#> code_A code_B weight
#> 1 A1 B1 0.5
#> 2 A1 B2 0.5
#> 3 A2 B1 1.0