knitr::opts_chunk$set( message = FALSE, warning = FALSE, collapse = TRUE, comment = "#>" )
The goal of CME.assistant is to store frequently used functions within the CM team.
options(rmarkdown.html_vignette.check_title = FALSE)
For now the most useful ones I used in many scripts are:
read.results.csv
: read any "results.csv"read.country.summary
: read any "Rates & Deaths_Country Summary.csv"read.region.summary
: read any "Rates & Deaths_...Region.csv"Examples:
# reproduce these code requires access to our internal Dropbox folders # Always a good idea to update the library to make sure you have the latest version # devtools::install_github("unicef-drp/CME.assistant") # library("CME.assistant") USERPROFILE <- CME.assistant::load_os_leading_dir() # leading dir to Dropbox # Dropbox directories to all results.csv dir_CC_code <- file.path(USERPROFILE, "Dropbox/UNICEF Work/Country consultation/Code_for_CC") source(file.path(dir_CC_code, "R/Dropbox_results_directories_2021.R")) dt_results <- rbindlist(lapply(results_dir_list_final_2021, CME.assistant::read.results.csv)) # which the same as: dt_results <- do.call(rbind, lapply(results_dir_list_final_2021, CME.assistant::read.results.csv)) dt_results[!is.na(value), table(Sex, Shortind)] # Dropbox directories to all final aggregates dir_report_code <- file.path(USERPROFILE, "Dropbox/UNICEF Work/IGME report etc/2021/Code_for_report/") source(file.path(dir_report_code, "Dropbox_aggresults_directories_2021.R")) dir_country_summary <- c( file.path(dir_aggu5, "Rates & Deaths_Country Summary.csv"), file.path(dir_aggu5_f, "Rates & Deaths(ADJUSTED)_female_Country Summary.csv"), file.path(dir_aggu5_m, "Rates & Deaths(ADJUSTED)_male_Country Summary.csv"), file.path(dir_agg10q5, "Rates & Deaths_Country Summary.csv"), file.path(dir_agg10q5_f, "Rates & Deaths(ADJUSTED)_Country Summary.csv"), file.path(dir_agg10q5_m, "Rates & Deaths(ADJUSTED)_Country Summary.csv"), file.path(dir_agg10q15, "Rates & Deaths_Country Summary.csv"), file.path(dir_agg10q15_f, "Rates & Deaths(ADJUSTED)_Country Summary.csv"), file.path(dir_agg10q15_m, "Rates & Deaths(ADJUSTED)_Country Summary.csv") ) dt_estimates <- rbindlist(lapply(dir_country_summary, CME.assistant::read.country.summary)) dt_estimates[, table(Shortind, Sex)]
Similar for regional summary:
region_group_filename <- "SDGSimpleRegion" dir_region_summary <- c( file.path(dir_aggu5, paste0("Rates & Deaths_", region_group_filename, ".csv")), file.path(dir_aggu5_f, paste0("Rates & Deaths(ADJUSTED)_female_", region_group_filename, ".csv")), file.path(dir_aggu5_m, paste0("Rates & Deaths(ADJUSTED)_male_", region_group_filename, ".csv")), file.path(dir_agg10q5, paste0("Rates & Deaths_", region_group_filename, ".csv")), file.path(dir_agg10q15, paste0("Rates & Deaths_", region_group_filename, ".csv")) ) dt_region <- rbindlist(lapply(dir_region_summary, CME.assistant::read.region.summary))
get.CME.UI.data
:
A more advanced version of read.country.summary
that allows more tuning: can output wide format by wide-quantile, wide-year, wide-indicator or one column for rate and one for death.c("ISO3Code", "OfficialName", "Shortind", "Year", "Quantile", "Sex", "value")
c_iso
) or year (year_range
) Examples:
dir_cs_u5 <- file.path(dir_aggu5, "Rates & Deaths_Country Summary.csv") dt_1 <- get.CME.UI.data(dir_file = dir_cs_u5) dt_1[Year == 2020][1:3,] dt_1 <- get.CME.UI.data(dir_file = dir_cs_u5, idvars = c("ISO3Code", "CountryName", "OfficialName"), format = "wide_q") dt_1[Year == 2020][1,] dt_1 <- get.CME.UI.data(dir_file = dir_cs_u5, format = "wide_q") dt_1[Year == 2020][1,] dt_1 <- get.CME.UI.data(dir_file = dir_cs_u5, format = "wide_ind", round_digit = 1) dt_1[Year == 2020][1:3,] dt_1 <- get.CME.UI.data(dir_file = dir_cs_u5, format = "wide_get", round_digit = 1) dt_1[Year == 2020][1:3,] dt_wy <- get.CME.UI.data(dir_file = dir_cs_u5, format = "wide_year", year_range = c(2000, 2010, 2020)) dt_wy[1:3,]
Using the wide-year output above, we can directly calculate ARR or percentage decline (PD)
dt_wy <- calculate.arr(dt_wy, 2000, 2010) # ARR dt_wy <- calculate.arr(dt_wy, 2010, 2020) # ARR dt_wy <- calculate.pd(dt_wy, 2000, 2020) # percentage decline dt_wy[Quantile == "Median" & Shortind == "NMR", ][1:3,]
Automatically point to the latest databases judging by the file names
Sometimes it is good to write down the directory of specific version of database. But these could be helpful when we just need to load the latest databases.
dir_IGME_input <- get.IGMEinput.dir(2022) dir_U5MR <- get.dir_U5MR(dir_IGME_input) dir_IMR <- get.dir_IMR(dir_IGME_input) dir_NMR <- get.dir_NMR(y5 = TRUE) # either 5-year or not dir_NMR <- get.dir_NMR(y5 = FALSE) # either 5-year or not dir_gender <- get.dir_gender(plotting = TRUE) # either dataset for plotting or modeling dir_gender <- get.dir_gender(plotting = FALSE) # either dataset for plotting or modeling
round.off
Almost used in every script
UNICEF_colors
The official color palette, help me to find UNICEF blue
str(UNICEF_colors)
hiv.iso
Sometimes useful
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.