Skip to content

Commit 0774401

Browse files
committed
fix wrong normalisation of row/col names
1 parent 8c65429 commit 0774401

4 files changed

Lines changed: 52 additions & 5 deletions

File tree

R/coefficient_matrix_create.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ coefficient_matrix_create <- function(
8989
data_table <- data_table[, 1:last_column]
9090
}
9191

92-
key_column <- tolower(as.character(unlist(data_table[, 1])))
92+
key_column <- as.character(unlist(data_table[, 1]))
9393

9494
# find denominator row
9595
if (total %in% c("output", "p1", "output_bp")) {

R/input_coefficient_matrix_create.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ input_coefficient_matrix_create <- function(data_table,
5252
cpa_total = "total"
5353
)
5454
for (nm in names(alias_map)) {
55-
if (nm %in% names(data_table) && !(alias_map[[nm]] %in% names(data_table))) {
55+
if (nm %in% names(data_table) &&
56+
!(alias_map[[nm]] %in% names(data_table))) {
5657
names(data_table)[names(data_table) == nm] <- alias_map[[nm]]
5758
}
5859
}
5960

6061
# --- Handle missing total row (toy tables) ----------------------------------
61-
if (!any(grepl("total|cpa_total|total output", tolower(data_table[[1]])))) {
62+
if (!any(grepl("total|cpa_total|total output",
63+
tolower(data_table[[1]])))) {
6264
return_part_arg <- NULL
6365
warning("No 'total' or 'cpa_total' row found; returning full matrix without subsetting.")
6466
} else {
@@ -73,9 +75,9 @@ input_coefficient_matrix_create <- function(data_table,
7375
digits = digits
7476
)
7577

76-
potential_total_names <- c("cpa_total", "total")
78+
potential_total_names <- c("cpa_total", "total", "TOTAL", "CPA_TOTAL")
7779

78-
key_column <- tolower(as.character(unlist(cm[, 1])))
80+
key_column <- as.character(unlist(cm[, 1]))
7981
remove_col <- which(names(cm) %in% potential_total_names)
8082
remove_row <- which(key_column %in% potential_total_names)
8183

R/iotable_get_eurostat.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,17 @@ iotable_get_eurostat <- function(
348348
version = 2
349349
)
350350
}
351+
352+
# Normalise row + column names to uppercase to ensure symmetry
353+
# and joinability if not the custom iotables snake case names are
354+
# used
355+
if (labelling != "iotables") {
356+
names(out) <- toupper(names(out))
357+
if ("prod_na" %in% names(out)) {
358+
out$prod_na <- toupper(out$prod_na)
359+
}
360+
}
361+
351362
out
352363
}
364+

tests/testthat/test-coefficient_matrix_create.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,36 @@ test_that("Households are handled correctly in coefficient_matrix_create", {
8989
expect_true(all(vapply(cm_with_hh[-1], is.numeric, logical(1))))
9090
expect_true(all(vapply(cm_no_hh[-1], is.numeric, logical(1))))
9191
})
92+
93+
94+
test_that("coefficient_matrix_create preserves row/column name casing", {
95+
96+
# A minimal symmetric toy table with precise uppercase CPA names
97+
small_io <- data.frame(
98+
prod_na = c("CPA_A01", "forestry", "CPA_A03", "TOTAL"),
99+
CPA_A01 = c(1, 2, 3, 6),
100+
forestry = c(4, 5, 6, 17),
101+
CPA_A03 = c(7, 8, 9, 24),
102+
TOTAL = c(12, 15, 18, 47),
103+
check.names = FALSE
104+
)
105+
106+
cm <- coefficient_matrix_create(small_io)
107+
108+
# Check that coefficient matrix still has uppercase names
109+
expect_identical(names(cm), names(small_io)[1:4])
110+
111+
# Check that row names in column 1 are preserved in uppercase
112+
expect_identical(
113+
as.character(cm$prod_na),
114+
as.character(small_io$prod_na)
115+
)
116+
117+
# Check that the set of row industry codes matches the set of column codes
118+
# (this confirms that row/column name matching was not broken)
119+
row_codes <- as.character(cm$prod_na[cm$prod_na != "output"])
120+
col_codes <- names(cm)[-1] # drop key column
121+
122+
expect_identical(sort(row_codes[1:3]), sort(col_codes))
123+
})
124+

0 commit comments

Comments
 (0)