Skip to content

Commit

Permalink
Use dplyr::case_when() rather than nested ifelse()
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisaloo committed Jul 7, 2023
1 parent 4883640 commit 118613c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 42 deletions.
17 changes: 7 additions & 10 deletions R/demographics.R
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,13 @@ describe_occupation <- function(isco_codes, output_level) {
path <- system.file("data", "isco88_table.rda", package = "epiCo")
load(path)
isco88_table <- isco88_table
input_level <- ifelse(isco_codes == 0 | isco_codes == 110, "Armed Forces",
ifelse(nchar(isco_codes) == 1, "major",
ifelse(nchar(isco_codes) == 2, "sub_major",
ifelse(nchar(isco_codes) == 3, "minor",
ifelse(nchar(isco_codes) == 4, "unit",
NA
)
)
)
)
input_level <- dplyr::case_when(
isco_codes %in% c(0, 110) ~ "Armed Forces",
nchar(isco_codes) == 1 ~ "major",
nchar(isco_codes) == 2 ~ "sub_major",
nchar(isco_codes) == 3 ~ "minor",
nchar(isco_codes) == 4 ~ "unit",
TRUE ~ NA_character_
)
tryCatch(
{
Expand Down
59 changes: 27 additions & 32 deletions R/endemic_channel.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,47 +81,42 @@ auto_endemic_channel <- function(disease_name, divipola_code, year,
## Dates and DIVIPOLA codes preparation and cleaning

disease_data <- disease_data %>% dplyr::mutate(
COD_MUN_R = ifelse(.data$COD_DPTO_R == 1,
.data$COD_PAIS_O, # 1 indicates residence abroad
ifelse(nchar(.data$COD_MUN_R) == 1,
COD_MUN_R = dplyr::case_when(
.data$COD_DPTO_R == 1 ~ .data$COD_PAIS_O, # 1 indicates residence abroad
nchar(.data$COD_MUN_R) == 1 ~
as.numeric(paste(.data$COD_DPTO_R,
.data$COD_MUN_R,
sep = "00"
)),
ifelse(nchar(.data$COD_MUN_R) == 2,
as.numeric(paste(.data$COD_DPTO_R,
.data$COD_MUN_R,
sep = "0"
)),
ifelse(nchar(.data$COD_MUN_R) == 3,
as.numeric(paste0(.data$COD_DPTO_R,
.data$COD_MUN_R
)),
NA
)
)
)
nchar(.data$COD_MUN_R) == 2 ~
as.numeric(paste(.data$COD_DPTO_R,
.data$COD_MUN_R,
sep = "0"
)),
nchar(.data$COD_MUN_R) == 3 ~
as.numeric(paste0(.data$COD_DPTO_R,
.data$COD_MUN_R
)),
TRUE ~ NA_real_
),
COD_MUN_O = ifelse(.data$COD_DPTO_O == 1,
.data$COD_PAIS_O, # 1 indicates infection occurred abroad
ifelse(nchar(.data$COD_MUN_O) == 1,
COD_MUN_O = dplyr::case_when(
# 1 indicates infection occurred abroad
.data$COD_DPTO_O == 1 ~ .data$COD_PAIS_O,
nchar(.data$COD_MUN_O) == 1 ~
as.numeric(paste(.data$COD_DPTO_O,
.data$COD_MUN_O,
sep = "00"
)),
ifelse(nchar(.data$COD_MUN_O) == 2,
as.numeric(paste(.data$COD_DPTO_O,
.data$COD_MUN_O,
sep = "0"
)),
ifelse(nchar(.data$COD_MUN_O) == 3,
as.numeric(paste0(.data$COD_DPTO_O,
.data$COD_MUN_O
)),
NA
)
)
)
nchar(.data$COD_MUN_O) == 2 ~
as.numeric(paste(.data$COD_DPTO_O,
.data$COD_MUN_O,
sep = "0"
)),
nchar(.data$COD_MUN_O) == 3 ~
as.numeric(paste0(.data$COD_DPTO_O,
.data$COD_MUN_O
)),
TRUE ~ NA_real_
),
EPI_WEEK = lubridate::epiweek(.data$FEC_NOT),
EPI_MONTH = lubridate::month(.data$FEC_NOT),
Expand Down

0 comments on commit 118613c

Please sign in to comment.