Skip to content

Commit 6eb0f99

Browse files
author
AAoritz
committed
bug fix: nuts_aggregate adds codes of all versions to check missings which yields in NAs even though complete
1 parent e50280e commit 6eb0f99

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

R/nuts_aggregate.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ nuts_aggregate <- function(data,
113113
if (nr_nuts_codes_recognized == 0)
114114
cli_abort("NUTS codes are not recognized and cannot be converted.")
115115

116+
116117
# CONVERSION POSSIBLE
117118
#----------------------
118119
# CONVERSION BETWEEN DIFFERENT NUTS LEVELS
@@ -138,6 +139,7 @@ nuts_aggregate <- function(data,
138139
n_rows_dropped <- data[["n_rows_dropped"]]
139140
message_multiple_versions <- data[["message_multiple_versions"]]
140141
data <- data[["data"]]
142+
from_version_string <- data$from_version[1]
141143

142144
# Prepare join with regional indicator stocks such that missing NUTS codes within groups are kept
143145
# - Create group structure
@@ -149,6 +151,9 @@ nuts_aggregate <- function(data,
149151
# - Prepare stocks from cross_walks for subsetting and matching
150152
cross_walks <- get("cross_walks")
151153
stocks <- cross_walks %>%
154+
# Subset to desired version
155+
filter(.data$from_version == from_version_string) %>%
156+
# Keep only stocks
152157
filter(.data$from_version == .data$to_version) %>%
153158
select(-c("from_version", "to_version", "to_code")) %>%
154159
distinct(.data$from_code, .keep_all = TRUE)
@@ -162,7 +167,6 @@ nuts_aggregate <- function(data,
162167
stocks_groups <- stocks %>% cross_join(groups)
163168
}
164169

165-
166170
# - Subset cross walks to desired countries, levels and versions
167171
stocks_groups <- stocks_groups %>%
168172
rename(from_level = .data$level) %>%
@@ -184,7 +188,7 @@ nuts_aggregate <- function(data,
184188
message_missing_codes <- c("x" = "{.blue {.red Missing} NUTS codes in data. No values are calculated for regions associated with missing NUTS codes. Ensure that the input data is complete.}")
185189

186190
} else if (nrow(missing) == 0) {
187-
message_missing_codes <- c("v" = "{.blue No {.red missing} NUTS codes.}")
191+
message_missing_codes <- c("v" = "{.blue No missing NUTS codes.}")
188192
}
189193
rm(missing)
190194

tests/testthat/test-nuts_aggregate.R

+40-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,45 @@ test_that("NUTS codes already at level 2", {
133133

134134
# Run positive tests
135135
#---------------------
136+
test_that("No missing NUTS in output, aggregation should go smooth", {
137+
expect_equal(
138+
all_nuts_codes %>%
139+
filter(country == "France", version == 2021, nchar(code) == 4) %>%
140+
select(nuts_code = code) %>%
141+
mutate(val = rnorm(nrow(.), 0, 1)) %>%
142+
nuts_classify(nuts_code = "nuts_code") %>%
143+
nuts_aggregate(
144+
to_level = 1,
145+
variables = c('val' = 'absolute')
146+
) %>%
147+
filter(is.na(val)) %>%
148+
nrow(.),
149+
0
150+
)
151+
})
152+
153+
test_that("Converter output spits out correct names", {
154+
expect_equal(
155+
manure %>%
156+
filter(nchar(geo) == 5) %>%
157+
filter(!grepl("EU|ME|ZZ", geo)) %>%
158+
nuts_classify(
159+
nuts_code = "geo",
160+
group_vars = c("indic_ag", "time")
161+
) %>%
162+
nuts_aggregate(
163+
to_level = 2,
164+
variables = c("values" = "absolute")
165+
) %>%
166+
names(.),
167+
c("to_code", "country", "indic_ag", "time", "values")
168+
)
169+
})
170+
171+
172+
173+
174+
136175
test_that("Converter output spits out correct names", {
137176
expect_equal(
138177
manure %>%
@@ -308,7 +347,7 @@ test_that("Feeding multiple NUTS versions within groups. Option most frequent.",
308347
) %>%
309348
filter(!is.na(values)) %>%
310349
dim(),
311-
c(52, 4)
350+
c(46, 4)
312351
)
313352
})
314353

0 commit comments

Comments
 (0)