From 7af1a1cecfc0968686fc4f6a34caf886cf22d9fb Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Fri, 7 Jul 2023 15:16:53 -0500 Subject: [PATCH] Use dplyr::case_when() rather than nested ifelse() --- R/demographics.R | 17 ++++++------- R/endemic_channel.R | 59 +++++++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/R/demographics.R b/R/demographics.R index 5e7df8d2..fa2c5a1f 100644 --- a/R/demographics.R +++ b/R/demographics.R @@ -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( { diff --git a/R/endemic_channel.R b/R/endemic_channel.R index cf9e12a2..57182f37 100644 --- a/R/endemic_channel.R +++ b/R/endemic_channel.R @@ -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),