Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All sector_classification datasets have no duplicate values of code column #327

Merged
merged 19 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion data-raw/classification_bridge.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,43 @@ isic_classification <- read_bridge(
)
use_data(isic_classification, overwrite = TRUE)

nace_classification <- read_bridge(
nace_classification_raw <- read_bridge(
file.path("data-raw", "nace_classification.csv")
)

nace_classification <- nace_classification_raw %>%
mutate(
prepend_value = case_when(
original_code %in% LETTERS ~ "",
trunc(as.numeric(original_code)) %in% seq(1, 3) ~ "A",
trunc(as.numeric(original_code)) %in% seq(5, 9) ~ "B",
trunc(as.numeric(original_code)) %in% seq(10, 33) ~ "C",
trunc(as.numeric(original_code)) == 35 ~ "D",
trunc(as.numeric(original_code)) %in% seq(36, 39) ~ "E",
trunc(as.numeric(original_code)) %in% seq(41, 43) ~ "F",
trunc(as.numeric(original_code)) %in% seq(45, 47) ~ "G",
trunc(as.numeric(original_code)) %in% seq(49, 53) ~ "H",
trunc(as.numeric(original_code)) %in% seq(55, 56) ~ "I",
trunc(as.numeric(original_code)) %in% seq(58, 63) ~ "J",
trunc(as.numeric(original_code)) %in% seq(64, 66) ~ "K",
trunc(as.numeric(original_code)) == 68 ~ "L",
trunc(as.numeric(original_code)) %in% seq(69, 75) ~ "M",
trunc(as.numeric(original_code)) %in% seq(77, 82) ~ "N",
trunc(as.numeric(original_code)) == 84 ~ "O",
trunc(as.numeric(original_code)) == 85 ~ "P",
trunc(as.numeric(original_code)) %in% seq(86, 88) ~ "Q",
trunc(as.numeric(original_code)) %in% seq(90, 93) ~ "R",
trunc(as.numeric(original_code)) %in% seq(94, 96) ~ "S",
trunc(as.numeric(original_code)) %in% seq(97, 98) ~ "T",
trunc(as.numeric(original_code)) == 99 ~ "U",
TRUE ~ "Z" #debug value, see unit tests)
)
) %>%
mutate(
code = paste0(prepend_value, original_code),
prepend_value = NULL
)

use_data(nace_classification, overwrite = TRUE)

naics_classification <- read_bridge(
Expand Down
2 changes: 1 addition & 1 deletion data-raw/cnb_classification.csv
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ INFORMATION AND COMMUNICATION,1,1100,not in scope,FALSE
INFORMATION AND COMMUNICATION,1,1100,not in scope,FALSE
INFORMATION AND COMMUNICATION,1,1100,not in scope,FALSE
INFORMATION AND COMMUNICATION,1,1100,not in scope,FALSE
MANUFACTURING,1,1200,not in scope,FALSE
MANUFACTURING,1,1200,steel,TRUE
MANUFACTURING,1,1200,steel,TRUE
MANUFACTURING,1,1200,steel,TRUE
MANUFACTURING,1,1200,steel,TRUE
Expand Down
3 changes: 1 addition & 2 deletions data-raw/gics_classification.csv
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ code,code_level,sector,borderline,description
151020,3,cement,TRUE,construction materials
151030,3,not in scope,FALSE,containers & packaging
151040,3,steel,TRUE,metals & mining
151040,3,coal,TRUE,metals & mining
151050,3,not in scope,FALSE,paper & forest products
201010,3,not in scope,FALSE,aerospace & defense
201020,3,cement,TRUE,building products
Expand Down Expand Up @@ -244,7 +243,7 @@ code,code_level,sector,borderline,description
402010,3,not in scope,FALSE,diversified financial services
402020,3,not in scope,FALSE,consumer finance
402030,3,not in scope,FALSE,capital markets
402040,3,not in scope,FALSE,"mortgage real estate investment
402040,3,not in scope,FALSE,"mortgage real estate investment
trusts (reits)"
403010,3,not in scope,FALSE,insurance
404010,3,not in scope,FALSE,real estate -- discontinued effective 04/28/2006
Expand Down
Binary file modified data/cnb_classification.rda
Binary file not shown.
Binary file modified data/gics_classification.rda
Binary file not shown.
Binary file modified data/isic_classification.rda
Binary file not shown.
Binary file modified data/nace_classification.rda
Binary file not shown.
Binary file modified data/naics_classification.rda
Binary file not shown.
Binary file modified data/psic_classification.rda
Binary file not shown.
Binary file modified data/sector_classifications.rda
Binary file not shown.
Binary file modified data/sic_classification.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion man/gics_classification.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/sector_classifications.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions tests/testthat/_snaps/sector_classifications.md

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions tests/testthat/test-classification_bridge.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ test_that("In classification datasets, `code` is of type 'character' (#185)", {
types <- unlist(lapply(datasets, function(x) typeof(x[["code"]])))
expect_equal(unique(types), "character")
})

test_that("In classification datasets, values of `code` are unique per sector and dataset (#229)", {
datasets <- enlist_datasets("r2dii.data", "classification")

#FIXME: This removes known offending datasets from the test.
# These datasets will be deprecated. Deprecation process tracked in #329
datasets$cnb_classification <- NULL
datasets$isic_classification <- NULL
datasets$sector_classifications <- NULL

no_duplicate_codes <- lapply(datasets, function(x) {
distinct_code_sector <- unique(x[c("code", "sector")])
result <- subset(as.data.frame(table(distinct_code_sector$code)),Freq > 1)
nrow(result) == 0
})

for (i in seq_along(no_duplicate_codes)) {
expect_true(no_duplicate_codes[[i]], info = names(no_duplicate_codes)[i])
}

})
5 changes: 5 additions & 0 deletions tests/testthat/test-sector_classifications.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ test_that("has `sector` values that are lowercase not uppercase", {
sectors <- sort(unique(sector_classifications$sector))
expect_equal(sectors, tolower(sectors))
})

test_that("debug value does not appear in `nace_classification`", {
nace_has_debug_value <- any(grepl("Z", nace_classification$original_code))
expect_false(nace_has_debug_value)
})
Loading