diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..80f8809 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index c833a2c..86fa0f8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .RData .Ruserdata inst/doc +.DS_Store diff --git a/NAMESPACE b/NAMESPACE index dcd4474..d8d9695 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,7 +7,9 @@ export(add_days_between) export(add_detailed_label) export(add_detailed_label_es) export(add_id_for_event) +export(add_id_for_municipality) export(add_id_for_president) +export(add_muni_gb2014_from_id) export(add_nameline) export(add_nameline_es) export(add_presidency_column) @@ -30,6 +32,7 @@ export(combine_names) export(count_events_in_month) export(count_ongoing_events) export(count_range_by) +export(department_name_from_id) export(displayed_date_string) export(estimated_date) export(estimated_date_string) @@ -48,16 +51,22 @@ export(fully_annotate_es) export(generate_examples_with_results) export(gray_pal) export(id_for_event) +export(id_for_municipality) +export(id_for_municipality_2) export(id_for_president) export(muni_counts_by) export(muni_list_with_counts_by) +export(municipality_name_from_id) +export(municipality_vector_from_id) export(n_categorized_by) export(n_filter) export(n_municipality) export(n_responsibility_by) export(perp_pal) +export(province_name_from_id) export(quasilogical_as_binary) export(red_pal) +export(rename_anexo_columns) export(render_age) export(render_age_es) export(render_presidency) @@ -70,6 +79,7 @@ export(set_maximums) export(share_of_largest_n) export(standard_filter) export(str_equivalent) +export(str_equivalent_list) export(string_to_listcase) export(sv_pal) export(top_values_string) @@ -90,10 +100,13 @@ importFrom(dplyr,if_any) importFrom(dplyr,is.tbl) importFrom(dplyr,left_join) importFrom(dplyr,mutate) +importFrom(dplyr,pull) importFrom(dplyr,relocate) +importFrom(dplyr,rename) importFrom(dplyr,rename_with) importFrom(dplyr,rowwise) importFrom(dplyr,select) +importFrom(dplyr,starts_with) importFrom(dplyr,summarize) importFrom(dplyr,ungroup) importFrom(forcats,fct_collapse) diff --git a/R/aaa-data.R b/R/aaa-data.R index 9df26be..856bae4 100644 --- a/R/aaa-data.R +++ b/R/aaa-data.R @@ -160,6 +160,92 @@ #' to "diciembre" "month.name.es" +#' Municipal Reference Table for Bolivia +#' +#' A reference table containing municipality information for Bolivia, +#' read from the official anexo of municipalities. +#' +#' @format A data frame with columns: +#' \describe{ +#' \item{codigo_ine}{Municipal code (INE code)} +#' \item{municipio}{Municipality name} +#' \item{provincia}{Province name} +#' \item{departamento}{Department name} +#' \item{superficie}{Area of the municipality in square kilometers} +#' \item{poblacion}{Population of the municipality} +#' \item{densidad}{Population density (people per square kilometer)} +#' \item{municipio_alt}{Alternative municipality name} +#' } +#' +#' @details +#' This table contains the official list of municipalities in Bolivia +#' with their corresponding INE (Instituto Nacional de Estadística) codes, +#' provinces, and departments. The table is used for municipality lookup +#' and ID assignment functions. +"anexo_municipios" + +#' Municipality ID Lookup Table +#' +#' A comprehensive lookup table for municipality IDs and names in Bolivia, +#' including alternative municipality names and mappings. +#' +#' @format A data frame with columns: +#' \describe{ +#' \item{id_muni}{Municipal ID (INE code)} +#' \item{muni_gb2014}{Municipality name in GB2014 map format} +#' \item{muni_anexo}{Standard municipality name from anexo} +#' \item{muni_ine}{Municipality name in INE table} +#' \item{muni_census}{Municipality name in census table} +#' \item{department}{Department name} +#' \item{muni_list}{List of alternative municipality names} +#' \item{n_unique}{Number of unique names for the municipality} +#' } +#' +#' @details +#' This table provides comprehensive municipality lookup functionality, +#' including alternative spellings and name variations. It supports +#' the id_for_municipality_2() function and other advanced municipality +#' matching operations. +"muni_id_lookup_table" + +#' Municipality Name Conversion Table for GB2014 Format +#' +#' A lookup table for converting between standard municipality names +#' and GB2014 format municipality names in Bolivia. +#' +#' @format A data frame with columns: +#' \describe{ +#' \item{id_muni}{Municipal ID (INE code)} +#' \item{muni_gb2014}{Municipality name in GB2014 format} +#' \item{muni_anexo}{Standard municipality name from anexo} +#' \item{recode}{R code string for recoding municipality names} +#' } +#' +#' @details +#' This table contains only municipalities where the GB2014 format name +#' differs from the standard anexo name. It includes generated R code +#' strings that can be used to perform the name conversions automatically. +"muni_gb2014_conversion" + +#' Municipality Lookup Table in GB2014 Format +#' +#' A simplified lookup table using GB2014 format municipality names +#' for municipality ID and department lookup. +#' +#' @format A data frame with columns: +#' \describe{ +#' \item{id_muni}{Municipal ID (INE code)} +#' \item{municipality}{Municipality name in GB2014 format} +#' \item{department}{Department name} +#' } +#' +#' @details +#' This table provides a streamlined lookup mechanism using GB2014 +#' format municipality names. It's derived from muni_id_lookup_table +#' and can be used when working with data that uses GB2014 naming +#' conventions. +"muni_gb2014_lookup" + # This list produced by colnames(deaths_aug24) %>% dput() # Needed for dplyr references to variables # See: https://www.r-bloggers.com/2019/08/no-visible-binding-for-global-variable/ @@ -228,7 +314,17 @@ utils::globalVariables( "date_text", "sr_text", "lev", - "standard_factoring_variables" + "standard_factoring_variables", + "muni_list", + "muni_anexo", + "muni_gb2014", + "anexo_municipios", + "muni_id_lookup_table", + "code", + "id_muni", + "municipio", + "provincia", + "departamento" ) ) diff --git a/R/add-presidency-column.R b/R/add-presidency-column.R index 9710f04..4180c34 100644 --- a/R/add-presidency-column.R +++ b/R/add-presidency-column.R @@ -93,6 +93,9 @@ add_presidency_column <- function(dataframe, variable, #' render_presidency("Gonzalo Sanchez de Lozada (2nd)", "id_presidency") # "p107" #' render_presidency("Gonzalo Sanchez de Lozada (2nd)", "presidency_surnames") #' render_presidency("Gonzalo Sanchez de Lozada (2nd)", "presidency_year_es") +#' render_presidency("p111", "presidency_commonname_es", source_var="id_presidency") +#' render_presidency("p111", "presidency_initials_num", source_var="id_presidency") +#' render_presidency("p111", "presidency_commonname", source_var="id_presidency") render_presidency <- function(value, dest_var, lookup_table = presidency_name_table, diff --git a/R/data-cleaning.R b/R/data-cleaning.R index 639fc43..d4a6b4d 100644 --- a/R/data-cleaning.R +++ b/R/data-cleaning.R @@ -64,14 +64,22 @@ assign_state_perpetrator_levels <- function(dataframe, simplify=FALSE){ de$state_perpetrator <- str_to_title(de$state_perpetrator) de$state_perpetrator <- fct_na_value_to_level(de$state_perpetrator, level = "Unknown") if (simplify){ + sp_levels_in_order <- c("Unknown", "No", "Indirect", "Mutiny", "Yes") + de$state_perpetrator <- fct_collapse(de$state_perpetrator, Yes = c("Yes", "Likely Yes", "Presumed Yes"), Indirect = c("Indirect"), No = c("No", "Likely No"), Mutiny = c("In Mutiny"), Unknown = c("Unknown", "Disputed", "Suspected") ) %>% suppressWarnings() + + if(length(levels(de$state_responsibility)) > length(lev$state_responsibility$levels)) { + warning(paste("Unknown state_perpetrator level:", + setdiff(levels(de$state_perpetrator), sp_levels_in_order), "\n")) + } + de$state_perpetrator <- fct_relevel(de$state_perpetrator, - c("Unknown", "No", "Indirect", "Mutiny", "Yes")) # default ordering + sp_levels_in_order) # default ordering } return(de) } @@ -136,6 +144,12 @@ assign_state_responsibility_levels <- function(dataframe, simplify=FALSE){ Unknown = c("Unknown", "Unclear", "Disputed") ) %>% suppressWarnings() + + if(length(levels(de$state_responsibility)) > length(lev$state_responsibility$levels)) { + warning(paste("Unknown state_responsibility level:", + setdiff(levels(de$state_responsibility), lev$state_responsibility$levels), "\n")) + } + de$state_responsibility <- fct_relevel(de$state_responsibility, lev$state_responsibility$levels) %>% suppressWarnings() diff --git a/R/id-for-municipality.R b/R/id-for-municipality.R new file mode 100644 index 0000000..e610e5d --- /dev/null +++ b/R/id-for-municipality.R @@ -0,0 +1,412 @@ +#' Rename Columns in a Dataframe for Standardization +#' +#' This function renames specific columns in a dataframe to standardize their names. +#' It ensures that the columns `municipality`, `province`, and `department` are present, renaming existing columns +#' (`municipio`, `provincia`, `departamento`) if necessary. Additionally, it renames any column starting with `codigo` +#' to `cod.mun`. +#' +#' @param dataframe A dataframe containing the columns to be standardized. +#' +#' @return A dataframe with standardized column names. +#' +#' @details +#' - The function checks for the presence of `municipality`, `province`, and `department`. +#' If these columns are missing, it renames `municipio`, `provincia`, and `departamento` to their English equivalents. +#' - Any column name starting with `codigo` is renamed to `cod.mun`. +#' +#' @examples +#' # Example dataframe +#' df <- data.frame( +#' municipio = c("La Paz", "Cochabamba"), +#' provincia = c("Pedro Domingo Murillo", "Cercado"), +#' departamento = c("La Paz", "Cochabamba"), +#' codigo123 = c(101, 102) +#' ) +#' +#' # Rename columns +#' renamed_df <- rename_anexo_columns(df) +#' print(names(renamed_df)) # Should include "municipality", "province", "department", "cod.mun" +#' +#' @importFrom dplyr rename rename_with starts_with pull mutate rowwise +#' @export +rename_anexo_columns <- function(dataframe) { + # Check if destination columns are missing and perform renaming accordingly + if (!"municipality" %in% names(dataframe)) { + dataframe <- dataframe %>% + dplyr::rename(municipality = municipio) + } + if (!"province" %in% names(dataframe)) { + dataframe <- dataframe %>% + dplyr::rename(province = provincia) + } + if (!"department" %in% names(dataframe)) { + dataframe <- dataframe %>% + dplyr::rename(department = departamento) + } + + # Rename "codigo" columns to "cod.mun" without suffix + dataframe <- dataframe %>% + dplyr::rename_with(~ gsub("^codigo.*$", "cod.mun", .), .cols = dplyr::starts_with("codigo")) + + return(dataframe) +} + +#' Get the ID of a municipality based on its name +#' +#' Returns the unique identifier for a municipality based on its name and +#' optionally the department. These identifiers are the codes used +#' in the Bolivian government databases, known as INE codes for the +#' Instituto Nacional de Estadística. +#' +#' The function will warn the user if a municipality name without +#' department corresponds to multiple municipalities. In this case, the first +#' municipality found will be returned. (There are about a dozen such cases in Bolivia.) +#' +#' `id_for_municipality_2` is a version of the function that handles lookup +#' tables with lists of municipalities. It is relies on `muni_id_lookup_table`. +#' +#' @param municipality_i Name of the municipality to search for. +#' @param department_i Optional name of the department to filter by. +#' @param muni_list_table A data frame containing the municipality list table. +#' +#' @return The unique identifier for the municipality as listed in the `code` = +#' column of the `muni_list_table`. If no match is found, returns an empty string. +#' +#' @export +#' @examples +#' id_for_municipality("La Paz") # 020101 +#' id_for_municipality("La Paz", "La Paz") # 020101 +#' id_for_municipality("La Paz", "Cochabamba") # empty string +#' id_for_municipality("Potosí", "Potosí") # 050101 +#' id_for_municipality("Potosi") # 050101 +#' id_for_municipality("San Ramón") # 071103 +#' id_for_municipality("San Ramón", "Santa Cruz") # 071103 +#' id_for_municipality("San Ramón", "Beni") # 080702 +#' +#' id_for_municipality_2("zudanez") # "010301" +#' id_for_municipality_2("villa montes") # "060303" +#' id_for_municipality_2("villamontes") # "060303" +#' id_for_municipality_2("San Ignacio") # "070301" +#' id_for_municipality_2("San Ignacio", "Beni") # "080501" +id_for_municipality <- function(municipality_i, department_i = "", muni_list_table = anexo_municipios) { + if (is.na(municipality_i)){ + return(as.character(NA)) + } + + # Rename columns if necessary + if ("municipio" %in% names(muni_list_table) && !"municipality" %in% names(muni_list_table)) { + muni_list_table <- rename_anexo_columns(muni_list_table) + } + + # Ensure the first column is named "code" and formatted as character with leading zeros + names(muni_list_table)[1] <- "code" + # reformat first column as a character with leading zero + if (is.numeric(dplyr::pull(muni_list_table,1))){ + muni_list_table <- muni_list_table %>% + dplyr::mutate(code = as.character(sprintf("%06d", code))) + } + + # Filter by municipality + municipality_row <- muni_list_table %>% + dplyr::filter(str_equivalent(municipality, municipality_i)) + # print((municipality_row)) + + if (nrow(municipality_row) == 0) { + warning(paste("Municipality", municipality_i, "not found. Returning empty string.")) + return("") + } + + # Further filter by department if provided + if (department_i != "") { + municipality_row <- municipality_row %>% + dplyr::filter(str_equivalent(department, department_i)) + if (nrow(municipality_row) == 0) { + warning(paste("Municipality", municipality_i, "not found in department", department_i, ". Returning empty string.")) + return("") + } + } + + # Handle multiple results + if (nrow(municipality_row) > 1) { + warning(paste("Multiple municipalities found for", municipality_i, + if (department_i != "") paste("in department", department_i) else "without department", + ". Returning first ID found.")) + } + + return(unname(municipality_row$code[1])) +} + +#' Add an unique identifier for municipality (id_muni) column to a dataset +#' +#' @param dataset A dataframe with a column containing the name of the +#' municipality, named `municipality`. +#' @param id_variable_name The name of the new column to be created. Default is +#' `id_muni`. +#' @param muni_table A data frame containing the municipality list table. +#' @param overwrite A boolean indicating whether to overwrite an existing +#' column. +#' +#' @return A dataframe with a new column `id_muni` containing the unique +#' identifier for the municipality, placed before the `municipality` column. +#' +#' @export +#' +#' @examples +#' id_for_municipality("Villa Ricardo Mugia - Icla", muni_list_table = muni_gb2014_lookup) +#' id_for_municipality_2("Villa Ricardo Mugia - Icla", "Chuquisaca") +add_id_for_municipality <- function(dataset, + id_variable_name = "id_muni", + muni_table = anexo_municipios, + overwrite = FALSE){ + if (!"municipality" %in% names(dataset)) { + stop("The dataset does not contain the 'municipality' column.") + } + if (id_variable_name %in% names(dataset) & + overwrite == FALSE) { + warning(paste("The dataset already contains the specified municipality id column. ", + "Call add_id_for_event() with overwrite = TRUE to replace it.")) + return(dataset) + } + + # dataset[[id_variable_name]] <- vapply(dataset$municipality, function(x) id_for_municipality(x, "", muni_table), + # FUN.VALUE = character(1)) + if ("muni_list" %in% names(muni_table)) { + # If the table has a list of municipalities, use id_for_municipality_2 + dataset[[id_variable_name]] <- vapply(seq_len(nrow(dataset)), function(i) { + id_for_municipality_2(dataset$municipality[i], dataset$department[i], muni_table) + }, FUN.VALUE = character(1)) + } else { + # Otherwise, use id_for_municipality + dataset[[id_variable_name]] <- vapply(seq_len(nrow(dataset)), function(i) { + id_for_municipality(dataset$municipality[i], dataset$department[i], muni_table) + }, FUN.VALUE = character(1)) + } + + dataset <- dataset %>% + dplyr::relocate(!!rlang::sym(id_variable_name), .before = municipality) + + return(dataset) +} + + +#' @rdname id_for_municipality +#' @export +id_for_municipality_2 <- function(municipality_i, department_i = "", muni_list_table = muni_id_lookup_table) { + if (is.na(municipality_i)){ + return(as.character(NA)) + } + + assertthat::assert_that( + is.character(municipality_i), + is.character(department_i), + is.data.frame(muni_list_table), + "muni_list" %in% names(muni_list_table) + ) + + # Filter by municipality + municipality_row <- muni_list_table %>% + dplyr::rowwise() %>% + dplyr::filter(str_equivalent_list(municipality_i, muni_list)) + + # Check if no rows match the municipality + if (nrow(municipality_row) == 0) { + warning(paste("Municipality", municipality_i, "not found. Returning empty string.")) + return("") + } + + # Further filter by department if provided + if (department_i != "") { + municipality_row <- municipality_row %>% + dplyr::filter(str_equivalent(department, department_i)) + if (nrow(municipality_row) == 0) { + warning(paste("Municipality", municipality_i, "not found in department", department_i, ". Returning empty string.")) + return("") + } + } + + # Handle multiple results + if (nrow(municipality_row) > 1) { + warning(paste("Multiple municipalities found for", municipality_i, + if (department_i != "") paste("in department", department_i) else "without department", + ". Returning first ID found.")) + } + + # Return the first matching id_muni + return(unname(municipality_row$id_muni[1])) +} + +#' Get Municipality Details from ID Vector +#' +#' This function looks up municipality details (municipality, province, and department) +#' from a list of municipality IDs in the given `muni_list_table`. +#' +#' @param id_muni A character vector of municipality IDs to look up. +#' @param muni_list_table A tibble or dataframe containing municipality information. +#' The table must have columns `cod.mun`, `municipality`, `province`, and `department`. +#' +#' @return A list with three components: +#' - `municipality`: A character vector of municipality names. +#' - `province`: A character vector of province names. +#' - `department`: A character vector of department names. +#' +#' @details +#' The function performs a lookup by matching the `id_muni` with the `cod.mun` column in `muni_list_table`. +#' If an ID is not found, the corresponding entries in the output list will be `NA`. +#' +#' @examples +#' # Example `muni_list_table` +#' anexo_municipios <- tibble::tibble( +#' cod.mun = c("010101", "010102", "010103"), +#' municipality = c("Sucre", "Yotala", "Poroma"), +#' province = c("Oropeza", "Oropeza", "Oropeza"), +#' department = c("Chuquisaca", "Chuquisaca", "Chuquisaca") +#' ) +#' +#' # Lookup municipality details +#' ids <- c("010101", "010102", "010999") +#' result <- municipality_name_from_id(ids, anexo_municipios) +#' print(result) +#' +#' @export +municipality_vector_from_id <- function(id_muni, muni_list_table=anexo_municipios) { + + # Rename columns if necessary + if ("municipio" %in% names(muni_list_table) && !"municipality" %in% names(muni_list_table)) { + muni_list_table <- rename_anexo_columns(muni_list_table) + } + + # Ensure the first column is named "code" and formatted as character with leading zeros + names(muni_list_table)[1] <- "code" + # reformat first column as a character with leading zero + if (is.numeric(dplyr::pull(muni_list_table,1))){ + muni_list_table <- muni_list_table %>% + dplyr::mutate(code = as.character(sprintf("%06d", code))) + } + # Check if id_muni is a character vector + if (is.numeric(id_muni)) { + id_muni <- as.character(sprintf("%06d", id_muni)) + } + + # Perform a lookup for each ID in the id_muni + matched_rows <- muni_list_table[match(id_muni, muni_list_table$code), ] + + # Extract the required columns and handle missing matches + municipality <- matched_rows$municipality + province <- matched_rows$province + department <- matched_rows$department + + # Replace missing values (rows not found) with NA + municipality[is.na(matched_rows$code)] <- NA + province[is.na(matched_rows$code)] <- NA + department[is.na(matched_rows$code)] <- NA + + # Return the result as a list + list( + municipality = municipality, + province = province, + department = department + ) +} +# Get the geographic names from municipal ID +# +# These function retrieve the municipality, province, and department names +#' from the unique identifier (ID) of a municipality. The ID is a code used +#' in the Bolivian government databases, known as INE codes for the +#' Instituto Nacional de Estadística. +#' +#' @param id_muni A character vector of municipality IDs to look up. +#' @param muni_list_table A lookup dataframe containing municipality information. +#' +#' @return A string with the name of the municipality, province, or department. +#' +#' @rdname municipality_vector_from_id +#' @export +#' +#' @examples +#' municipality_name_from_id("020101") # "La Paz" +#' municipality_name_from_id("050101") # "Potosí" +#' municipality_name_from_id("071103") # "San Ramón" +#' municipality_name_from_id("080702") # "San Ramón" +#' municipality_name_from_id("010301") # "Villa Zudáñez" +#' province_name_from_id("020101") # "Murillo" +#' province_name_from_id("050101") # "Tomás Frías" +#' province_name_from_id("071103") # "Ñuflo de Chaves" +#' province_name_from_id("080702") # "Mamoré" +#' province_name_from_id("010301") # "Zudáñez" +#' province_name_from_id("060303") # "Gran Chaco" +#' province_name_from_id("070301") # "Velasco" +#' province_name_from_id("080501") # "Moxos" +#' department_name_from_id("020101") # "La Paz" +#' department_name_from_id("050101") # "Potosí" +#' department_name_from_id("071103") # "Santa Cruz" +#' department_name_from_id("080702") # "Beni" +#' department_name_from_id("010301") # "Chuquisaca" +#' department_name_from_id("060303") # "Tarija" +#' department_name_from_id("070301") # "Santa Cruz" +#' department_name_from_id("080501") # "Beni" +municipality_name_from_id <- function(id_muni, muni_list_table=anexo_municipios) { + vector <- municipality_vector_from_id(id_muni, muni_list_table) + # Return the municipality names + vector$municipality +} + +#' @rdname municipality_vector_from_id +#' @export +province_name_from_id <- function(id_muni, muni_list_table=anexo_municipios) { + vector <- municipality_vector_from_id(id_muni, muni_list_table) + # Return the province names + vector$province +} + +#' @rdname municipality_vector_from_id +#' @export +department_name_from_id <- function(id_muni, muni_list_table=anexo_municipios) { + vector <- municipality_vector_from_id(id_muni, muni_list_table) + # Return the department names + vector$department +} + +#' Add Municipality GB2014 Code from ID +#' +#' This function adds a column `muni_gb2014` to a dataframe based on the `id_muni` column. +#' It uses a lookup table to map `id_muni` to `muni_gb2014`. +#' +#' @param dataframe A dataframe containing a column `id_muni`. +#' @param muni_table A lookup table containing `id_muni` and `muni_gb2014` columns. +#' @return A dataframe with an additional column `muni_gb2014` containing the corresponding GB2014 codes. +#' +#' @export +add_muni_gb2014_from_id <- function(dataframe, muni_table = muni_id_lookup_table){ + # Check if the dataframe has a column named "id_muni" + if (!"id_muni" %in% names(dataframe)) { + stop("The dataframe does not contain a column named 'id_muni'.") + } + # Check if the muni_table has column named "id_muni" and "muni_gb2014" + if (!"id_muni" %in% names(muni_table)) { + stop("The lookup does not contain a column named 'id_muni'.") + } + if (!"muni_gb2014" %in% names(muni_table)) { + stop("The lookup does not contain a column named 'muni_gb2014'.") + } + + lookup_table <- muni_table %>% + dplyr::select(id_muni, muni_gb2014) %>% + dplyr::distinct() + + # Join the lookup table with the dataframe + dataframe <- dataframe %>% + dplyr::left_join(lookup_table, by = "id_muni") + + return(dataframe) +} + +# deaths_aug24 %>% add_id_for_municipality() %>% +# add_muni_gb2014_from_id() %>% +# select(event_title, municipality, muni_gb2014) %>% +# filter(municipality != muni_gb2014) +# de %>% add_id_for_municipality() %>% +# add_muni_gb2014_from_id() %>% +# select(event_title, municipality, muni_gb2014) %>% +# filter(municipality != muni_gb2014) %>% +# distinct(municipality, muni_gb2014) + diff --git a/R/str-equivalent.R b/R/str-equivalent.R index 3742b77..89839e7 100644 --- a/R/str-equivalent.R +++ b/R/str-equivalent.R @@ -20,3 +20,29 @@ str_equivalent <- function(x, y) { y <- stringi::stri_trans_general(y, "Latin-ASCII") stringr::str_equal(x, y, ignore_case = TRUE) } + +#' Check for Equivalent Strings in a List +#' +#' This function checks if any string in a list is equivalent to a target string, +#' considering case insensitivity, accent removal, whitespace trimming, and non-breaking space replacement. +#' +#' @param string A character string to compare. +#' @param string_list A character vector of potential matches. +#' +#' @return A logical value: \code{TRUE} if any string in the list is equivalent to the target string, +#' or \code{FALSE} otherwise. +#' +#' @details +#' The function uses \code{\link{str_equivalent}} to determine string equivalence and applies it +#' to each element of the list. +#' +#' @examples +#' str_equivalent_list("café", c("cafe", "tea", "coffee")) # Returns TRUE +#' str_equivalent_list("hello", c("world", "hi")) # Returns FALSE +#' +#' @seealso \code{\link{str_equivalent}} +#' @export +str_equivalent_list <- function(string, string_list) { + # Check if any member of string_list is equivalent to string using str_equivalent + any(sapply(string_list, function(x) str_equivalent(string, x))) +} \ No newline at end of file diff --git a/data-raw/anexo_municipios_de_bolivia.csv b/data-raw/anexo_municipios_de_bolivia.csv new file mode 100644 index 0000000..201130e --- /dev/null +++ b/data-raw/anexo_municipios_de_bolivia.csv @@ -0,0 +1,341 @@ +"codigo_ine","municipio","provincia","departamento","superficie","poblacion","densidad","municipio_alt" +"010101","Sucre","Oropeza","Chuquisaca",1820,296125,162.71,NA +"010102","Yotala","Oropeza","Chuquisaca",452.9,9241,20.4,NA +"010103","Poroma","Oropeza","Chuquisaca",1395,13640,9.78,NA +"010201","Azurduy","Azurduy","Chuquisaca",1700,7861,4.62,NA +"010202","Tarvita","Azurduy","Chuquisaca",1334,16470,12.35,NA +"010301","Villa Zudáñez","Zudáñez","Chuquisaca",701.1,12687,18.1,NA +"010302","Presto","Zudáñez","Chuquisaca",1329,12023,9.05,NA +"010303","Villa Mojocoya","Zudáñez","Chuquisaca",1247,8971,7.19,NA +"010304","Icla","Zudáñez","Chuquisaca",846.3,8854,10.46,NA +"010401","Padilla","Tomina","Chuquisaca",1620,7542,4.66,NA +"010402","Tomina","Tomina","Chuquisaca",826.9,8341,10.09,NA +"010403","Sopachuy","Tomina","Chuquisaca",620.4,6886,11.1,NA +"010404","Villa Alcalá","Tomina","Chuquisaca",312.8,3752,11.99,NA +"010405","El Villar","Tomina","Chuquisaca",895.5,5636,6.29,NA +"010501","Monteagudo","Hernando Siles","Chuquisaca",3387,24487,7.23,NA +"010502","Huacareta","Hernando Siles","Chuquisaca",2962,7201,2.43,NA +"010601","Tarabuco","Yamparáez","Chuquisaca",1029,13251,12.88,NA +"010602","Yamparáez","Yamparáez","Chuquisaca",598.8,8537,14.26,NA +"010701","Camargo","Nor Cinti","Chuquisaca",1990,14712,7.39,NA +"010702","San Lucas","Nor Cinti","Chuquisaca",3872,26486,6.84,NA +"010703","Incahuasi","Nor Cinti","Chuquisaca",870,16311,18.75,NA +"010704","Villa Charcas","Nor Cinti","Chuquisaca",1000,13924,13.92,NA +"010801","Villa Serrano","Belisario Boeto","Chuquisaca",1713,8961,5.23,NA +"010901","Villa Abecia","Sud Cinti","Chuquisaca",707.3,5371,7.59,NA +"010902","Culpina","Sud Cinti","Chuquisaca",3910,14868,3.8,NA +"010903","Las Carreras","Sud Cinti","Chuquisaca",994.6,5007,5.03,NA +"011001","Villa Vaca Guzmán","Luis Calvo","Chuquisaca",3793,13196,3.48,"Muyupampa" +"011002","Huacaya","Luis Calvo","Chuquisaca",1186,2546,2.15,NA +"011003","Macharetí","Luis Calvo","Chuquisaca",7880,7245,0.92,NA +"020101","La Paz","Murillo","La Paz",2001,755732,377.68,NA +"020102","Palca","Murillo","La Paz",743.7,21641,29.1,NA +"020103","Mecapaca","Murillo","La Paz",533.1,20455,38.37,NA +"020104","Achocalla","Murillo","La Paz",196.2,46068,234.8,NA +"020105","El Alto","Murillo","La Paz",351.3,885035,2519.31,NA +"020201","Achacachi","Omasuyos","La Paz",700,47064,67.23,NA +"020202","Ancoraimes","Omasuyos","La Paz",329.5,16900,51.29,NA +"020203","Chua Cocani","Omasuyos","La Paz",17.5,8657,494.69,NA +"020204","Huarina","Omasuyos","La Paz",205,9258,45.16,NA +"020205","Santiago de Huata","Omasuyos","La Paz",125,5096,40.77,NA +"020206","Huatajata","Omasuyos","La Paz",31,7183,231.71,NA +"020301","Coro Coro","Pacajes","La Paz",1145,13395,11.7,NA +"020302","Caquiaviri","Pacajes","La Paz",1586,15464,9.75,NA +"020303","Calacoto","Pacajes","La Paz",3958,12057,3.05,NA +"020304","Comanche","Pacajes","La Paz",436.9,5201,11.9,NA +"020305","Charaña","Pacajes","La Paz",2925,4124,1.41,NA +"020306","Waldo Ballivián","Pacajes","La Paz",123.5,6557,53.09,NA +"020307","Nazacara de Pacajes","Pacajes","La Paz",19.2,3990,207.81,NA +"020308","Callapa","Pacajes","La Paz",1236,7965,6.44,NA +"020401","Puerto Acosta","Camacho","La Paz",260,13940,53.62,NA +"020402","Mocomoco","Camacho","La Paz",531.6,17653,33.21,NA +"020403","Puerto Carabuco","Camacho","La Paz",415,17248,41.56,NA +"020404","Humanata","Camacho","La Paz",450,5615,12.48,NA +"020405","Escoma","Camacho","La Paz",130,8810,67.77,NA +"020501","Chuma","Muñecas","La Paz",599.8,17671,29.46,NA +"020502","Ayata","Muñecas","La Paz",671.4,8957,13.34,NA +"020503","Aucapata","Muñecas","La Paz",243.1,6720,27.64,NA +"020601","Sorata","Larecaja","La Paz",2087,30020,14.38,NA +"020602","Guanay","Larecaja","La Paz",3965,16373,4.13,NA +"020603","Tacacoma","Larecaja","La Paz",826.9,9705,11.74,NA +"020604","Quiabaya","Larecaja","La Paz",133.6,4221,31.59,NA +"020605","Combaya","Larecaja","La Paz",99.5,3502,35.2,NA +"020606","Tipuani","Larecaja","La Paz",300.9,7640,25.39,NA +"020607","Mapiri","Larecaja","La Paz",1381,21805,15.79,NA +"020608","Teoponte","Larecaja","La Paz",1599,13469,8.42,NA +"020701","Apolo","Franz Tamayo","La Paz",13970,19047,1.36,NA +"020702","Pelechuco","Franz Tamayo","La Paz",2549,8993,3.53,NA +"020801","Viacha","Ingavi","La Paz",857.3,113453,132.34,NA +"020802","Guaqui","Ingavi","La Paz",191.6,8599,44.88,NA +"020803","Tiahuanaco","Ingavi","La Paz",352.2,13568,38.52,NA +"020804","Desaguadero","Ingavi","La Paz",91.3,7867,86.17,NA +"020805","San Andrés de Machaca","Ingavi","La Paz",1528,7832,5.13,NA +"020806","Jesús de Machaca","Ingavi","La Paz",970,16957,17.48,NA +"020807","Taraco","Ingavi","La Paz",110.2,7511,68.16,NA +"020901","Luribay","Loayza","La Paz",509,10581,20.79,NA +"020902","Sapahaqui","Loayza","La Paz",846.8,16420,19.39,NA +"020903","Yaco","Loayza","La Paz",599.1,12303,20.54,NA +"020904","Malla","Loayza","La Paz",329.7,4866,14.76,NA +"020905","Cairoma","Loayza","La Paz",543.8,13657,25.11,NA +"021001","Inquisivi","Inquisivi","La Paz",3277,18764,5.73,NA +"021002","Quime","Inquisivi","La Paz",878.5,11184,12.73,NA +"021003","Cajuata","Inquisivi","La Paz",971.1,15485,15.95,NA +"021004","Colquiri","Inquisivi","La Paz",1060,23456,22.13,NA +"021005","Ichoca","Inquisivi","La Paz",852,9536,11.19,NA +"021006","Licoma Pampa","Inquisivi","La Paz",150.1,7791,51.91,NA +"021101","Chulumani","Sud Yungas","La Paz",286.6,18278,63.78,NA +"021102","Irupana","Sud Yungas","La Paz",1389,23076,16.61,NA +"021103","Yanacachi","Sud Yungas","La Paz",579.8,7583,13.08,NA +"021104","Palos Blancos","Sud Yungas","La Paz",3605,26995,7.49,NA +"021105","La Asunta","Sud Yungas","La Paz",2832,45586,16.1,NA +"021201","Pucarani","Los Andes","La Paz",940.8,49701,52.83,NA +"021202","Laja","Los Andes","La Paz",690.4,38612,55.93,NA +"021203","Batallas","Los Andes","La Paz",993.9,20503,20.63,NA +"021204","Puerto Pérez","Los Andes","La Paz",126.8,7874,62.1,NA +"021301","Sica Sica","Aroma","La Paz",1742,33546,19.26,NA +"021302","Umala","Aroma","La Paz",860.4,9021,10.48,NA +"021303","Ayo Ayo","Aroma","La Paz",491.6,11251,22.89,NA +"021304","Calamarca","Aroma","La Paz",495.6,15279,30.83,NA +"021305","Patacamaya","Aroma","La Paz",570.9,25049,43.88,NA +"021306","Colquencha","Aroma","La Paz",305.3,12344,40.43,NA +"021307","Collana","Aroma","La Paz",125.6,5525,43.99,NA +"021401","Coroico","Nor Yungas","La Paz",952.1,27531,28.92,NA +"021402","Coripata","Nor Yungas","La Paz",346.2,23910,69.06,NA +"021501","Ixiamas","Iturralde","La Paz",37453,12487,0.33,NA +"021502","San Buenaventura","Iturralde","La Paz",2839,10115,3.56,NA +"021601","Charazani","Bautista Saavedra","La Paz",2592,15854,6.12,NA +"021602","Curva","Bautista Saavedra","La Paz",601.2,6423,10.68,NA +"021701","Copacabana","Manco Kapac","La Paz",177.1,17431,98.42,NA +"021702","San Pedro de Tiquina","Manco Kapac","La Paz",61.2,9097,148.64,NA +"021703","Tito Yupanqui","Manco Kapac","La Paz",14.9,7512,504.16,NA +"021801","San Pedro de Curahuara","Gualberto Villarroel","La Paz",733.3,9044,12.33,NA +"021802","Papel Pampa","Gualberto Villarroel","La Paz",914.1,6222,6.81,NA +"021803","Chacarilla","Gualberto Villarroel","La Paz",370,2031,5.49,NA +"021901","Santiago de Machaca","General José Manuel Pando","La Paz",1276,6766,5.3,NA +"021902","Catacora","General José Manuel Pando","La Paz",563.2,5326,9.46,NA +"022001","Caranavi","Caranavi","La Paz",2100,59034,28.11,NA +"022002","Alto Beni","Caranavi","La Paz",950,10499,11.05,NA +"030101","Cochabamba","Cercado","Cochabamba",289.3,661484,2286.5,NA +"030201","Aiquile","Campero","Cochabamba",2723,23000,8.45,NA +"030202","Pasorapa","Campero","Cochabamba",2389,6468,2.71,NA +"030203","Omereque","Campero","Cochabamba",759.3,6678,8.79,NA +"030301","Independencia","Ayopaya","Cochabamba",1514,21717,14.34,NA +"030302","Morochata","Ayopaya","Cochabamba",600,14598,24.33,NA +"030303","Cocapata","Ayopaya","Cochabamba",6800,19467,2.86,NA +"030401","Tarata","Esteban Arze","Cochabamba",338.4,11235,33.2,NA +"030402","Anzaldo","Esteban Arze","Cochabamba",646.6,7322,11.32,NA +"030403","Arbieto","Esteban Arze","Cochabamba",147.2,30454,206.89,NA +"030404","Sacabamba","Esteban Arze","Cochabamba",185.7,4953,26.67,NA +"030501","Arani","Arani","Cochabamba",216.3,11411,52.76,NA +"030502","Vacas","Arani","Cochabamba",360.9,8425,23.34,NA +"030601","Arque","Arque","Cochabamba",516.8,10635,20.58,NA +"030602","Tacopaya","Arque","Cochabamba",554.9,10549,19.01,NA +"030701","Capinota","Capinota","Cochabamba",542.9,19291,35.53,NA +"030702","Santiváñez","Capinota","Cochabamba",307.3,7868,25.6,NA +"030703","Sicaya","Capinota","Cochabamba",132.7,3949,29.76,NA +"030801","Cliza","Germán Jordán","Cochabamba",54.3,20877,384.48,NA +"030802","Toco","Germán Jordán","Cochabamba",63.8,7208,112.98,NA +"030803","Tolata","Germán Jordán","Cochabamba",84,7372,87.76,NA +"030901","Quillacollo","Quillacollo","Cochabamba",565.3,165830,293.35,NA +"030902","Sipe Sipe","Quillacollo","Cochabamba",480.5,55601,115.71,NA +"030903","Tiquipaya","Quillacollo","Cochabamba",344.4,61600,178.86,NA +"030904","Vinto","Quillacollo","Cochabamba",217.9,55773,255.96,NA +"030905","Colcapirhua","Quillacollo","Cochabamba",28.8,66235,2299.83,NA +"031001","Sacaba","Chapare","Cochabamba",701.8,218502,311.35,NA +"031002","Colomi","Chapare","Cochabamba",559.7,19318,34.51,NA +"031003","Villa Tunari","Chapare","Cochabamba",14561,95588,6.56,NA +"031101","Tapacarí","Tapacarí","Cochabamba",1651,24534,14.86,NA +"031201","Totora","Carrasco","Cochabamba",2157,14766,6.85,NA +"031202","Pojo","Carrasco","Cochabamba",2392,11840,4.95,NA +"031203","Pocona","Carrasco","Cochabamba",838.6,12216,14.57,NA +"031204","Chimoré","Carrasco","Cochabamba",2740,27374,9.99,NA +"031205","Puerto Villarroel","Carrasco","Cochabamba",2241,64345,28.71,NA +"031206","Entre Ríos","Carrasco","Cochabamba",2101,45520,21.67,NA +"031301","Mizque","Mizque","Cochabamba",1895,19846,10.47,NA +"031302","Vila Vila","Mizque","Cochabamba",601.6,5220,8.68,NA +"031303","Alalay","Mizque","Cochabamba",423.1,3461,8.18,NA +"031401","Punata","Punata","Cochabamba",85.9,34813,405.27,NA +"031402","Villa Rivero","Punata","Cochabamba",103,8551,83.02,NA +"031403","San Benito","Punata","Cochabamba",141.7,12924,91.21,NA +"031404","Tacachi","Punata","Cochabamba",12.4,1276,102.9,NA +"031405","Cuchumuela","Punata","Cochabamba",63.6,3818,60.03,NA +"031501","Bolívar","Bolívar","Cochabamba",724,8197,11.32,NA +"031601","Tiraque","Tiraque","Cochabamba",2650,18190,6.86,NA +"031602","Shinahota","Tiraque","Cochabamba",737.81,27067,36.69,NA +"040101","Oruro","Cercado","Oruro",276.2,297497,1077.11,NA +"040102","Caracollo","Cercado","Oruro",2364,23068,9.76,NA +"040103","El Choro","Cercado","Oruro",1119,10393,9.29,NA +"040104","Paria","Cercado","Oruro",1373,15675,11.42,NA +"040201","Challapata","Abaroa","Oruro",2586,35339,13.67,NA +"040202","Quillacas","Abaroa","Oruro",921.7,5105,5.54,NA +"040301","Corque","Carangas","Oruro",3380,16020,4.74,NA +"040302","Choquecota","Carangas","Oruro",849.9,2980,3.51,NA +"040401","Curahuara de Carangas","Sajama","Oruro",2866,6671,2.33,NA +"040402","Turco","Sajama","Oruro",4895,5277,1.08,NA +"040501","Huachacalla","Litoral","Oruro",20.8,3974,191.06,NA +"040502","Escara","Litoral","Oruro",1166,4194,3.6,NA +"040503","Cruz de Machacamarca","Litoral","Oruro",524.5,1511,2.88,NA +"040504","Yunguyo de Litoral","Litoral","Oruro",176.8,882,4.99,NA +"040505","Esmeralda","Litoral","Oruro",606.1,3883,6.41,NA +"040601","Poopó","Poopó","Oruro",704.2,9152,13,NA +"040602","Pazña","Poopó","Oruro",740,4220,5.7,NA +"040603","Antequera","Poopó","Oruro",283.9,3614,12.73,NA +"040701","Huanuni","Dalence","Oruro",553.1,20028,36.21,NA +"040702","Machacamarca","Dalence","Oruro",306.8,6096,19.87,NA +"040801","Salinas de Garci Mendoza","Ladislao Cabrera","Oruro",4924,15906,3.23,NA +"040802","Pampa Aullagas","Ladislao Cabrera","Oruro",1062,3180,2.99,NA +"040901","Sabaya","Sabaya","Oruro",3384,15387,4.55,NA +"040902","Coipasa","Sabaya","Oruro",204.2,1405,6.88,NA +"040903","Chipaya","Sabaya","Oruro",300.1,2366,7.88,NA +"041001","Toledo","Saucarí","Oruro",2928,12115,4.14,NA +"041101","Eucaliptus","Tomás Barrón","Oruro",333.8,5443,16.31,NA +"041201","Santiago de Andamarca","Sud Carangas","Oruro",2321,6300,2.71,NA +"041202","Belén de Andamarca","Sud Carangas","Oruro",1080,2996,2.77,NA +"041301","Totora","San Pedro de Totora","Oruro",1443,6570,4.55,NA +"041401","Santiago de Huari","Sebastián Pagador","Oruro",2407,13439,5.58,NA +"041501","La Rivera","Mejillones","Oruro",198.2,1266,6.39,NA +"041502","Todos Santos","Mejillones","Oruro",238.1,1132,4.75,NA +"041503","Carangas","Mejillones","Oruro",257.7,1130,4.38,NA +"041601","Huayllamarca","Nor Carangas","Oruro",876.7,5980,6.82,NA +"050101","Potosí","Tomás Frías","Potosí",1233,218336,177.08,NA +"050102","Tinguipaya","Tomás Frías","Potosí",1464,28829,19.69,NA +"050103","Yocalla","Tomás Frías","Potosí",880.7,11119,12.63,NA +"050104","Urmiri","Tomás Frías","Potosí",1304,3162,2.42,NA +"050201","Uncía","Rafael Bustillo","Potosí",1000,22673,22.67,NA +"050202","Chayanta","Rafael Bustillo","Potosí",614.5,16008,26.05,NA +"050203","Llallagua","Rafael Bustillo","Potosí",536,41571,77.56,NA +"050204","Chuquihuta","Rafael Bustillo","Potosí",150,8170,54.47,NA +"050301","Betanzos","Cornelio Saavedra","Potosí",1909,31772,16.64,NA +"050302","Chaquí","Cornelio Saavedra","Potosí",454.1,9262,20.4,NA +"050303","Tacobamba","Cornelio Saavedra","Potosí",833.9,14208,17.04,NA +"050401","Colquechaca","Chayanta","Potosí",800,16163,20.2,NA +"050402","Ravelo","Chayanta","Potosí",1241,16728,13.48,NA +"050403","Pocoata","Chayanta","Potosí",1192,32307,27.1,NA +"050404","Ocurí","Chayanta","Potosí",941.7,14367,15.26,NA +"050405","San Pedro de Macha","Chayanta","Potosí",842,20933,24.86,NA +"050501","San Pedro de Buena Vista","Charcas","Potosí",2170,27961,12.89,NA +"050502","Toro Toro","Charcas","Potosí",1176,10719,9.11,NA +"050601","Cotagaita","Nor Chichas","Potosí",6435,29070,4.52,NA +"050602","Vitichi","Nor Chichas","Potosí",1756,10102,5.75,NA +"050701","Sacaca","Alonso de Ibáñez","Potosí",892.5,17188,19.26,NA +"050702","Caripuyo","Alonso de Ibáñez","Potosí",518.1,8427,16.27,NA +"050801","Tupiza","Sud Chichas","Potosí",6204,45531,7.34,NA +"050802","Atocha","Sud Chichas","Potosí",2108,9364,4.44,NA +"050901","Colcha K","Nor Lípez","Potosí",15904,12638,0.79,NA +"050902","San Pedro de Quemes","Nor Lípez","Potosí",4403,1603,0.36,NA +"051001","San Pablo de Lípez","Sud Lípez","Potosí",15321,3136,0.2,NA +"051002","Mojinete","Sud Lípez","Potosí",407.2,1022,2.51,NA +"051003","San Antonio de Esmoruco","Sud Lípez","Potosí",2512,1941,0.77,NA +"051101","Puna","Linares","Potosí",1400,16350,11.68,NA +"051102","Caiza D","Linares","Potosí",1376,12891,9.37,NA +"051103","Ckochas","Linares","Potosí",2100,14301,6.81,NA +"051201","Uyuni","Quijarro","Potosí",7784,35118,4.51,NA +"051202","Tomave","Quijarro","Potosí",8239,7811,0.95,NA +"051203","Porco","Quijarro","Potosí",1149,14626,12.73,NA +"051301","Arampampa","General Bilbao","Potosí",424.7,3623,8.53,NA +"051302","Acasio","General Bilbao","Potosí",345.3,4434,12.84,NA +"051401","Llica","Daniel Campos","Potosí",5812,4150,0.71,NA +"051402","Tahua","Daniel Campos","Potosí",1105,1561,1.41,NA +"051501","Villazón","Modesto Omiste","Potosí",2469,49312,19.97,NA +"051601","San Agustín","Enrique Baldivieso","Potosí",2116,2119,1,NA +"060101","Tarija","Cercado","Tarija",2132,238942,112.07,NA +"060201","Padcaya","Arce","Tarija",4431,17783,4.01,NA +"060202","Bermejo","Arce","Tarija",381,36967,97.03,NA +"060301","Yacuiba","Gran Chaco","Tarija",3641,97577,26.8,NA +"060302","Caraparí","Gran Chaco","Tarija",3126,15650,5.01,NA +"060303","Villa Montes","Gran Chaco","Tarija",11784,46010,3.9,NA +"060401","Uriondo","José María Avilés","Tarija",801.6,15954,19.9,NA +"060402","Yunchará","José María Avilés","Tarija",1834,4763,2.6,NA +"060501","San Lorenzo","Méndez","Tarija",1969,30352,15.41,NA +"060502","El Puente","Méndez","Tarija",2057,9504,4.62,NA +"060601","Entre Ríos","O'Connor","Tarija",5033,20846,4.14,NA +"070101","Santa Cruz de la Sierra","Andrés Ibáñez","Santa Cruz",1269,1606671,1266.09,NA +"070102","Cotoca","Andrés Ibáñez","Santa Cruz",619.5,106292,171.58,NA +"070103","Porongo","Andrés Ibáñez","Santa Cruz",943.5,23016,24.39,NA +"070104","La Guardia","Andrés Ibáñez","Santa Cruz",953.2,147622,154.87,NA +"070105","El Torno","Andrés Ibáñez","Santa Cruz",959.5,55558,57.9,NA +"070201","Warnes","Warnes","Santa Cruz",1315,150803,114.68,NA +"070202","Okinawa Uno","Warnes","Santa Cruz",1042,10508,10.08,NA +"070301","San Ignacio de Velasco","Velasco","Santa Cruz",49102,67610,1.38,NA +"070302","San Miguel de Velasco","Velasco","Santa Cruz",9544,15177,1.59,NA +"070303","San Rafael de Velasco","Velasco","Santa Cruz",9731,8786,0.9,NA +"070401","Buena Vista","Ichilo","Santa Cruz",2250,13586,6.04,NA +"070402","San Carlos","Ichilo","Santa Cruz",1212,18998,15.67,NA +"070403","Villa Yapacaní","Ichilo","Santa Cruz",9514,57683,6.06,NA +"070404","San Juan de Yapacaní","Ichilo","Santa Cruz",1578,10206,6.47,NA +"070501","San José de Chiquitos","Chiquitos","Santa Cruz",19121,40961,2.14,NA +"070502","Pailón","Chiquitos","Santa Cruz",13726,54357,3.96,NA +"070503","Roboré","Chiquitos","Santa Cruz",7353,18701,2.54,NA +"070601","Portachuelo","Sara","Santa Cruz",1076,20709,19.25,NA +"070602","Santa Rosa del Sara","Sara","Santa Cruz",4114,17984,4.37,NA +"070603","Colpa Bélgica","Sara","Santa Cruz",291.5,6020,20.65,NA +"070701","Lagunillas","Cordillera","Santa Cruz",1122,5910,5.27,NA +"070702","Charagua","Cordillera","Santa Cruz",71360,39262,0.55,NA +"070703","Cabezas","Cordillera","Santa Cruz",5058,31160,6.16,NA +"070704","Cuevo","Cordillera","Santa Cruz",872.5,4953,5.68,NA +"070705","Gutiérrez","Cordillera","Santa Cruz",2871,15169,5.28,"Kereimba Iyambae" +"070706","Camiri","Cordillera","Santa Cruz",1036,32656,31.52,NA +"070707","Boyuibe","Cordillera","Santa Cruz",1660,5603,3.38,NA +"070801","Vallegrande","Vallegrande","Santa Cruz",3188,17172,5.39,NA +"070802","El Trigal","Vallegrande","Santa Cruz",398.5,2367,5.94,NA +"070803","Moro Moro","Vallegrande","Santa Cruz",673.9,2811,4.17,NA +"070804","Postrervalle","Vallegrande","Santa Cruz",1157,1796,1.55,NA +"070805","Pucará","Vallegrande","Santa Cruz",682.3,2009,2.94,NA +"070901","Samaipata","Florida","Santa Cruz",2188,11814,5.4,NA +"070902","Pampagrande","Florida","Santa Cruz",1429,9650,6.75,NA +"070903","Mairana","Florida","Santa Cruz",922.4,12732,13.8,NA +"070904","Quirusillas","Florida","Santa Cruz",286.6,3095,10.8,NA +"071001","Montero","Obispo Santistevan","Santa Cruz",315,127544,404.9,NA +"071002","General Saavedra","Obispo Santistevan","Santa Cruz",529.4,16043,30.3,NA +"071003","Mineros","Obispo Santistevan","Santa Cruz",420.1,22731,54.11,NA +"071004","Fernández Alonso","Obispo Santistevan","Santa Cruz",760.1,14625,19.24,NA +"071005","San Pedro","Obispo Santistevan","Santa Cruz",3081,15158,4.92,NA +"071101","Concepción","Ñuflo de Chaves","Santa Cruz",35109,30109,0.86,NA +"071102","San Javier","Ñuflo de Chaves","Santa Cruz",3381,14897,4.41,NA +"071103","San Ramón","Ñuflo de Chaves","Santa Cruz",492.3,6346,12.89,NA +"071104","San Julián","Ñuflo de Chaves","Santa Cruz",5726,44635,7.8,NA +"071105","San Antonio de Lomerío","Ñuflo de Chaves","Santa Cruz",2503,8427,3.37,NA +"071106","Cuatro Cañadas","Ñuflo de Chaves","Santa Cruz",4486,25260,5.63,NA +"071201","San Matías","Ángel Sandóval","Santa Cruz",27082,15336,0.57,NA +"071301","Comarapa","Caballero","Santa Cruz",2900,17132,5.91,NA +"071302","Saipina","Caballero","Santa Cruz",489.5,6057,12.37,NA +"071401","Puerto Suárez","Germán Busch","Santa Cruz",12982,17378,1.34,NA +"071402","Puerto Quijarro","Germán Busch","Santa Cruz",1450,17826,12.29,NA +"071403","El Carmen Rivero Tórrez","Germán Busch","Santa Cruz",11011,8731,0.79,NA +"071501","Ascensión de Guarayos","Guarayos","Santa Cruz",8674,34002,3.92,NA +"071502","Urubichá","Guarayos","Santa Cruz",10470,6528,0.62,NA +"071503","El Puente","Guarayos","Santa Cruz",6745,17214,2.55,NA +"080101","Trinidad","Cercado","Beni",2539,124357,48.98,NA +"080102","San Javier","Cercado","Beni",8173,7654,0.94,NA +"080201","Riberalta","Vaca Díez","Beni",9726,107141,11.02,NA +"080202","Guayaramerín","Vaca Díez","Beni",6502,40759,6.27,NA +"080301","Reyes","General José Ballivián","Beni",12642,11274,0.89,NA +"080302","San Borja","General José Ballivián","Beni",13489,45562,3.38,NA +"080303","Santa Rosa","General José Ballivián","Beni",12106,10910,0.9,NA +"080304","Rurrenabaque","General José Ballivián","Beni",2529,21018,8.31,NA +"080401","Santa Ana del Yacuma","Yacuma","Beni",20093,18102,0.9,NA +"080402","Exaltación","Yacuma","Beni",24973,7890,0.32,NA +"080501","San Ignacio de Moxos","Moxos","Beni",20011,21578,1.08,NA +"080601","Loreto","Marbán","Beni",5364,4263,0.79,NA +"080602","San Andrés","Marbán","Beni",9355,15635,1.67,NA +"080701","San Joaquín","Mamoré","Beni",8692,6851,0.79,NA +"080702","San Ramón","Mamoré","Beni",10079,5441,0.54,NA +"080703","Puerto Siles","Mamoré","Beni",2071,1095,0.53,NA +"080801","Magdalena","Iténez","Beni",13634,12769,0.94,NA +"080802","Baures","Iténez","Beni",17616,6564,0.37,NA +"080803","Huacaraje","Iténez","Beni",4464,4628,1.04,NA +"090101","Cobija","Nicolás Suárez","Pando",451.1,54386,120.56,NA +"090102","Porvenir","Nicolás Suárez","Pando",10967,8970,0.82,NA +"090103","Bolpebra","Nicolás Suárez","Pando",2592,2390,0.92,NA +"090104","Bella Flor","Nicolás Suárez","Pando",5793,3632,0.63,NA +"090201","Puerto Rico","Manuripi","Pando",5320,7217,1.36,NA +"090202","San Pedro","Manuripi","Pando",2636,3239,1.23,NA +"090203","Filadelfia","Manuripi","Pando",11886,7941,0.67,NA +"090301","Puerto Gonzalo Moreno","Madre de Dios","Pando",1293,12006,9.29,NA +"090302","San Lorenzo","Madre de Dios","Pando",3171,9203,2.9,NA +"090303","Sena","Madre de Dios","Pando",7634,10264,1.34,NA +"090401","Santa Rosa del Abuná","Abuná","Pando",3890,2855,0.73,NA +"090402","Ingavi","Abuná","Pando",5430,2584,0.48,NA +"090501","Nueva Esperanza","General Federico Román","Pando",3651,1390,0.38,NA +"090502","Villa Nueva","General Federico Román","Pando",2823,2500,0.89,NA +"090503","Santos Mercado","General Federico Román","Pando",6623,2184,0.33,NA diff --git a/data-raw/muni-id-tables.R b/data-raw/muni-id-tables.R new file mode 100644 index 0000000..6d920a6 --- /dev/null +++ b/data-raw/muni-id-tables.R @@ -0,0 +1,21 @@ +anexo_municipios <- readr::read_csv(here::here("data-raw", "anexo_municipios_de_bolivia.csv")) +usethis::use_data(anexo_municipios, overwrite = TRUE) + +muni_id_lookup_table <- readr::read_rds(here::here("data-raw", "muni_id_lookup_table.rds")) +usethis::use_data(muni_id_lookup_table, overwrite = TRUE) + +# This code produces a substantial lookup table for changes needed between +# our standard municipalities and gb2014. +muni_gb2014_conversion <- muni_id_lookup_table %>% + filter(muni_gb2014 != muni_anexo) %>% + select(1, 3, 2) %>% + mutate(recode = str_glue("mutate(municipality = recode(municipality, \"{muni_anexo}\"", + " = ", + "\"{muni_gb2014}\")) %>% ")) +# muni_gb2014_conversion$recode +usethis::use_data(muni_gb2014_conversion, overwrite = TRUE) + +muni_gb2014_lookup <- muni_id_lookup_table %>% + select(id_muni, muni_gb2014, department) %>% + rename(municipality = muni_gb2014) +usethis::use_data(muni_gb2014_lookup, overwrite = TRUE) diff --git a/data-raw/muni_id_lookup_table.rds b/data-raw/muni_id_lookup_table.rds new file mode 100644 index 0000000..94d7688 Binary files /dev/null and b/data-raw/muni_id_lookup_table.rds differ diff --git a/data-raw/presidency-name-table.csv b/data-raw/presidency-name-table.csv index d952cdf..adbfb4e 100644 --- a/data-raw/presidency-name-table.csv +++ b/data-raw/presidency-name-table.csv @@ -1,29 +1,29 @@ -presidency,id_presidency,presidency_year,presidency_commonname,presidency_fullname,presidency_surnames,presidency_year_es,presidency_commonname_es,presidency_fullname_es,presidency_initials,presidency_initials_num,first_day,last_day -René Barrientos,p86,René Barrientos (1964-1969),René Barrientos,René Barrientos Ortuño,Barrientos Ortuño,René Barrientos (1964-1969),René Barrientos,René Barrientos Ortuño,RB,RB,1964-11-05,1966-08-06 -Luis Adolfo Siles Salinas,p87,Luis Adolfo Siles Salinas (1969),Luis Adolfo Siles Salinas,Luis Adolfo Siles Salinas,Siles Salinas,Luis Adolfo Siles Salinas (1969),Luis Adolfo Siles Salinas,Luis Adolfo Siles Salinas,LAS,LAS,1966-08-06,1969-04-27 -Alfredo Ovando Candía,p88,Alfredo Ovando (1969-1970),Alfredo Ovando,Alfredo Ovando Candía,Ovando Candía,Alfredo Ovando (1969-1970),Alfredo Ovando,Alfredo Ovando Candía,AO,AO,1969-04-27,1969-09-26 -Juan José Torres,p89,Juan José Torres (1970-1971),Juan José Torres,Juan José Torres González,Torres González,Juan José Torres (1970-1971),Juan José Torres,Juan José Torres González,JJT,JJT,1970-10-07,1971-08-21 -Hugo Banzer (1st),p90,Hugo Banzer (1971-1978),Hugo Banzer,Hugo Banzer Suárez,Banzer Suárez,Hugo Banzer (1971-1978),Hugo Banzer,Hugo Banzer Suárez,HB,HB 1,1971-08-22,1978-07-21 -Juan Pereda,p91,Juan Pereda (1978),Juan Pereda,Juan Pereda Asbún,Pereda Asbún,Juan Pereda (1978),Juan Pereda,Juan Pereda Asbún,JP,JP,1978-07-21,1978-11-24 -David Padilla (Chairman of the Military Junta),p92,David Padilla (1978-1979),David Padilla,David Padilla Arancibia,Padilla Arancibia,David Padilla (1978-1979),David Padilla,David Padilla Arancibia,DP,DP,1978-11-24,1979-08-08 -Wálter Guevara,p93,Walter Guevara (1979),Walter Guevara,Wálter Guevara Arze,Guevara Arze,Walter Guevara (1979),Walter Guevara,Wálter Guevara Arze,WG,WG,1979-08-08,1979-11-01 -Alberto Natusch,p94,Alberto Natusch (1979),Alberto Natusch,Alberto Natusch Busch,Natusch Busch,Alberto Natusch (1979),Alberto Natusch,Alberto Natusch Busch,AN,AN,1979-11-01,1979-11-16 -Lydia Gueiler Tejada,p95,Lidia Gueiler (1979-1980),Lidia Gueiler,Lidia Gueiler Tejada,Gueiler Tejada,Lidia Gueiler (1979-1980),Lidia Gueiler,Lidia Gueiler Tejada,LG,LG,1979-11-17,1980-07-18 -Luis García Meza Tejada (2nd),p96,Luis García Meza (1980-1981),Luis García Meza,Luis García Meza Tejada,García Meza Tejada,Luis García Meza (1980-1981),Luis García Meza,Luis García Meza Tejada,LGM,LGM,1980-07-18,1981-08-04 -Junta of Commanders of the Armed Forces 1981,p97,Junta of Commanders of the Armed Forces (1981),Military Junta (1981),Junta of Commanders of the Armed Forces of the Nation,Junta of Commanders of the Armed Forces of the Nation,Junta Militar de Comandantes de las Fuerzas Armadas (1981),Military Junta (1981),Junta Militar de Comandantes de las Fuerzas Armadas ,Junta,Junta 1981,1981-08-04,1981-09-04 -Celso Torrelio,p98,Celso Torrelio (1981-1982),Celso Torrelio,Celso Torrelio Villa,Torrelio Villa,Celso Torrelio (1981-1982),Celso Torrelio,Celso Torrelio Villa,CT,CT,1981-09-04,1982-07-19 -Junta of Commanders of the Armed Forces 1982,p99,Junta of Commanders of the Armed Forces (1982),Military Junta (1982),Junta of Commanders of the Armed Forces of the Nation,Junta of Commanders of the Armed Forces of the Nation,Junta Militar de Comandantes de las Fuerzas Armadas (1982),Military Junta (1982),Junta Militar de Comandantes de las Fuerzas Armadas ,Junta,Junta 1982,1982-07-19,1982-07-21 -Guido Vildoso,p100,Guido Vildoso (1982),Guido Vildoso,Guido Vildoso Calderón,Vildoso Calderón,Guido Vildoso (1982),Guido Vildoso,Guido Vildoso Calderón,GV,GV,1982-07-21,1982-10-10 -Hernán Siles Zuazo,p101,Hernán Siles Zuazo (1982-1985),Hernán Siles Zuazo,Hernán Siles Zuazo,Siles Zuazo,Hernán Siles Zuazo (1982-1985),Hernán Siles Zuazo,Hernán Siles Zuazo,HSZ,HSZ,1982-10-10,1985-08-06 -Víctor Paz Estenssoro,p102,Víctor Paz Estenssoro (1985-1989),Víctor Paz Estenssoro,Víctor Paz Estenssoro,Paz Estenssoro,Víctor Paz Estenssoro (1985-1989),Víctor Paz Estenssoro,Víctor Paz Estenssoro,VPE,VPE 2,1985-08-06,1989-08-06 -Jaime Paz Zamora,p103,Jaime Paz Zamora (1989-1993),Jaime Paz Zamora,Jaime Paz Zamora,Paz Zamora,Jaime Paz Zamora (1989-1993),Jaime Paz Zamora,Jaime Paz Zamora,JPZ,JPZ,1989-08-06,1993-08-06 -Gonzalo Sanchez de Lozada (1st),p104,Gonzalo Sánchez de Lozada (1993-1997),Gonzalo Sánchez de Lozada,Gonzalo Sánchez de Lozada,Sánchez de Lozada,Gonzalo Sánchez de Lozada (1993-1997),Gonzalo Sánchez de Lozada,Gonzalo Sánchez de Lozada,GSL,GSL 1,1993-08-06,1997-08-06 -Hugo Banzer (2nd),p105,Hugo Banzer (1997-2001),Hugo Banzer,Hugo Banzer Suárez,Banzer Suárez,Hugo Banzer (1997-2001),Hugo Banzer,Hugo Banzer Suárez,HB,HB 2,1997-08-06,2001-08-07 -Jorge Quiroga,p106,Jorge Quiroga (2001-2002),Jorge Quiroga,Jorge Quiroga Ramírez,Quiroga Ramírez,Jorge Quiroga (2001-2002),Jorge Quiroga,Jorge Quiroga Ramírez,JQ,JQ,2001-08-07,2002-08-06 -Gonzalo Sanchez de Lozada (2nd),p107,Gonzalo Sánchez de Lozada (2002-2003),Gonzalo Sánchez de Lozada,Gonzalo Sánchez de Lozada,Sánchez de Lozada,Gonzalo Sánchez de Lozada (2002-2003),Gonzalo Sánchez de Lozada,Gonzalo Sánchez de Lozada,GSL,GSL 2,2002-08-06,2003-10-17 -Carlos Diego Mesa Gisbert,p108,Carlos Mesa (2003-2005),Carlos Mesa,Carlos Diego Mesa Gisbert,Mesa Gisbert,Carlos Mesa (2003-2005),Carlos Mesa,Carlos Diego Mesa Gisbert,CM,CM,2003-10-17,2005-06-09 -Eduardo Rodríguez,p109,Eduardo Rodríguez (2005-2006),Eduardo Rodríguez,Eduardo Rodríguez Veltzé,Rodríguez Veltzé,Eduardo Rodríguez (2005-2006),Eduardo Rodríguez,Eduardo Rodríguez Veltzé,ER,ER,2005-06-09,2006-01-22 -Evo Morales,p110,Evo Morales (2006-2019),Evo Morales,Juan Evo Morales Ayma,Morales Ayma,Evo Morales (2006-2019),Evo Morales,Juan Evo Morales Ayma,EM,EM,2006-01-22,2019-11-10 -Interim military government,p111,Interim military government (2019),Military government (2019),Interim military government,Interim military government,Gobierno interino militar (2019),Military government (2019),Gobierno interino militar,Mil,Mil,2019-11-10,2019-11-12 -Jeanine Áñez,p112,Jeanine Áñez (2019-2020),Jeanine Áñez,Jeanine Áñez Chávez,Áñez Chávez,Jeanine Áñez (2019-2020),Jeanine Áñez,Jeanine Áñez Chávez,JÁ,JÁ,2019-11-12,2020-11-08 -Luis Arce,p113,Luis Arce (2020- ),Luis Arce,Luis Alberto Arce Catacora,Arce Catacora,Luis Arce (2020- ),Luis Arce,Luis Alberto Arce Catacora,LA,LA,2020-11-08,2025-03-20 \ No newline at end of file +presidency,id_presidency,presidency_year,presidency_commonname,presidency_fullname,presidency_surnames,presidency_year_es,presidency_commonname_es,presidency_fullname_es,presidency_initials,presidency_initials_num,first_day,last_day +René Barrientos,p86,René Barrientos (1964-1969),René Barrientos,René Barrientos Ortuño,Barrientos Ortuño,René Barrientos (1964-1969),René Barrientos,René Barrientos Ortuño,RB,RB,1964-11-05,1966-08-06 +Luis Adolfo Siles Salinas,p87,Luis Adolfo Siles Salinas (1969),Luis Adolfo Siles Salinas,Luis Adolfo Siles Salinas,Siles Salinas,Luis Adolfo Siles Salinas (1969),Luis Adolfo Siles Salinas,Luis Adolfo Siles Salinas,LAS,LAS,1966-08-06,1969-04-27 +Alfredo Ovando Candía,p88,Alfredo Ovando (1969-1970),Alfredo Ovando,Alfredo Ovando Candía,Ovando Candía,Alfredo Ovando (1969-1970),Alfredo Ovando,Alfredo Ovando Candía,AO,AO,1969-04-27,1969-09-26 +Juan José Torres,p89,Juan José Torres (1970-1971),Juan José Torres,Juan José Torres González,Torres González,Juan José Torres (1970-1971),Juan José Torres,Juan José Torres González,JJT,JJT,1970-10-07,1971-08-21 +Hugo Banzer (1st),p90,Hugo Banzer (1971-1978),Hugo Banzer,Hugo Banzer Suárez,Banzer Suárez,Hugo Banzer (1971-1978),Hugo Banzer,Hugo Banzer Suárez,HB,HB 1,1971-08-22,1978-07-21 +Juan Pereda,p91,Juan Pereda (1978),Juan Pereda,Juan Pereda Asbún,Pereda Asbún,Juan Pereda (1978),Juan Pereda,Juan Pereda Asbún,JP,JP,1978-07-21,1978-11-24 +David Padilla (Chairman of the Military Junta),p92,David Padilla (1978-1979),David Padilla,David Padilla Arancibia,Padilla Arancibia,David Padilla (1978-1979),David Padilla,David Padilla Arancibia,DP,DP,1978-11-24,1979-08-08 +Wálter Guevara,p93,Walter Guevara (1979),Walter Guevara,Wálter Guevara Arze,Guevara Arze,Walter Guevara (1979),Walter Guevara,Wálter Guevara Arze,WG,WG,1979-08-08,1979-11-01 +Alberto Natusch,p94,Alberto Natusch (1979),Alberto Natusch,Alberto Natusch Busch,Natusch Busch,Alberto Natusch (1979),Alberto Natusch,Alberto Natusch Busch,AN,AN,1979-11-01,1979-11-16 +Lydia Gueiler Tejada,p95,Lidia Gueiler (1979-1980),Lidia Gueiler,Lidia Gueiler Tejada,Gueiler Tejada,Lidia Gueiler (1979-1980),Lidia Gueiler,Lidia Gueiler Tejada,LG,LG,1979-11-17,1980-07-18 +Luis García Meza Tejada (2nd),p96,Luis García Meza (1980-1981),Luis García Meza,Luis García Meza Tejada,García Meza Tejada,Luis García Meza (1980-1981),Luis García Meza,Luis García Meza Tejada,LGM,LGM,1980-07-18,1981-08-04 +Junta of Commanders of the Armed Forces 1981,p97,Junta of Commanders of the Armed Forces (1981),Military Junta (1981),Junta of Commanders of the Armed Forces of the Nation,Junta of Commanders of the Armed Forces of the Nation,Junta Militar de Comandantes de las Fuerzas Armadas (1981),Military Junta (1981),Junta Militar de Comandantes de las Fuerzas Armadas ,Junta,Junta 1981,1981-08-04,1981-09-04 +Celso Torrelio,p98,Celso Torrelio (1981-1982),Celso Torrelio,Celso Torrelio Villa,Torrelio Villa,Celso Torrelio (1981-1982),Celso Torrelio,Celso Torrelio Villa,CT,CT,1981-09-04,1982-07-19 +Junta of Commanders of the Armed Forces 1982,p99,Junta of Commanders of the Armed Forces (1982),Military Junta (1982),Junta of Commanders of the Armed Forces of the Nation,Junta of Commanders of the Armed Forces of the Nation,Junta Militar de Comandantes de las Fuerzas Armadas (1982),Military Junta (1982),Junta Militar de Comandantes de las Fuerzas Armadas ,Junta,Junta 1982,1982-07-19,1982-07-21 +Guido Vildoso,p100,Guido Vildoso (1982),Guido Vildoso,Guido Vildoso Calderón,Vildoso Calderón,Guido Vildoso (1982),Guido Vildoso,Guido Vildoso Calderón,GV,GV,1982-07-21,1982-10-10 +Hernán Siles Zuazo,p101,Hernán Siles Zuazo (1982-1985),Hernán Siles Zuazo,Hernán Siles Zuazo,Siles Zuazo,Hernán Siles Zuazo (1982-1985),Hernán Siles Zuazo,Hernán Siles Zuazo,HSZ,HSZ,1982-10-10,1985-08-06 +Víctor Paz Estenssoro,p102,Víctor Paz Estenssoro (1985-1989),Víctor Paz Estenssoro,Víctor Paz Estenssoro,Paz Estenssoro,Víctor Paz Estenssoro (1985-1989),Víctor Paz Estenssoro,Víctor Paz Estenssoro,VPE,VPE 2,1985-08-06,1989-08-06 +Jaime Paz Zamora,p103,Jaime Paz Zamora (1989-1993),Jaime Paz Zamora,Jaime Paz Zamora,Paz Zamora,Jaime Paz Zamora (1989-1993),Jaime Paz Zamora,Jaime Paz Zamora,JPZ,JPZ,1989-08-06,1993-08-06 +Gonzalo Sanchez de Lozada (1st),p104,Gonzalo Sánchez de Lozada (1993-1997),Gonzalo Sánchez de Lozada,Gonzalo Sánchez de Lozada,Sánchez de Lozada,Gonzalo Sánchez de Lozada (1993-1997),Gonzalo Sánchez de Lozada,Gonzalo Sánchez de Lozada,GSL,GSL 1,1993-08-06,1997-08-06 +Hugo Banzer (2nd),p105,Hugo Banzer (1997-2001),Hugo Banzer,Hugo Banzer Suárez,Banzer Suárez,Hugo Banzer (1997-2001),Hugo Banzer,Hugo Banzer Suárez,HB,HB 2,1997-08-06,2001-08-07 +Jorge Quiroga,p106,Jorge Quiroga (2001-2002),Jorge Quiroga,Jorge Quiroga Ramírez,Quiroga Ramírez,Jorge Quiroga (2001-2002),Jorge Quiroga,Jorge Quiroga Ramírez,JQ,JQ,2001-08-07,2002-08-06 +Gonzalo Sanchez de Lozada (2nd),p107,Gonzalo Sánchez de Lozada (2002-2003),Gonzalo Sánchez de Lozada,Gonzalo Sánchez de Lozada,Sánchez de Lozada,Gonzalo Sánchez de Lozada (2002-2003),Gonzalo Sánchez de Lozada,Gonzalo Sánchez de Lozada,GSL,GSL 2,2002-08-06,2003-10-17 +Carlos Diego Mesa Gisbert,p108,Carlos Mesa (2003-2005),Carlos Mesa,Carlos Diego Mesa Gisbert,Mesa Gisbert,Carlos Mesa (2003-2005),Carlos Mesa,Carlos Diego Mesa Gisbert,CM,CM,2003-10-17,2005-06-09 +Eduardo Rodríguez,p109,Eduardo Rodríguez (2005-2006),Eduardo Rodríguez,Eduardo Rodríguez Veltzé,Rodríguez Veltzé,Eduardo Rodríguez (2005-2006),Eduardo Rodríguez,Eduardo Rodríguez Veltzé,ER,ER,2005-06-09,2006-01-22 +Evo Morales,p110,Evo Morales (2006-2019),Evo Morales,Juan Evo Morales Ayma,Morales Ayma,Evo Morales (2006-2019),Evo Morales,Juan Evo Morales Ayma,EM,EM,2006-01-22,2019-11-10 +Interim military government,p111,Interim military government (2019),Military government (2019),Interim military government,Interim military government,Gobierno interino militar (2019),Gobierno interino militar (2019),Gobierno interino militar,Mil,Mil,2019-11-10,2019-11-12 +Jeanine Áñez,p112,Jeanine Áñez (2019-2020),Jeanine Áñez,Jeanine Áñez Chávez,Áñez Chávez,Jeanine Áñez (2019-2020),Jeanine Áñez,Jeanine Áñez Chávez,JÁ,JÁ,2019-11-12,2020-11-08 +Luis Arce,p113,Luis Arce (2020- ),Luis Arce,Luis Alberto Arce Catacora,Arce Catacora,Luis Arce (2020- ),Luis Arce,Luis Alberto Arce Catacora,LA,LA,2020-11-08,2025-03-20 diff --git a/data/anexo_municipios.rda b/data/anexo_municipios.rda new file mode 100644 index 0000000..981ad9c Binary files /dev/null and b/data/anexo_municipios.rda differ diff --git a/data/muni_gb2014_conversion.rda b/data/muni_gb2014_conversion.rda new file mode 100644 index 0000000..ca5c055 Binary files /dev/null and b/data/muni_gb2014_conversion.rda differ diff --git a/data/muni_gb2014_lookup.rda b/data/muni_gb2014_lookup.rda new file mode 100644 index 0000000..28b1ac6 Binary files /dev/null and b/data/muni_gb2014_lookup.rda differ diff --git a/data/muni_id_lookup_table.rda b/data/muni_id_lookup_table.rda new file mode 100644 index 0000000..a60f9d2 Binary files /dev/null and b/data/muni_id_lookup_table.rda differ diff --git a/data/presidency_name_table.rda b/data/presidency_name_table.rda index a9f4e76..8d6bea4 100644 Binary files a/data/presidency_name_table.rda and b/data/presidency_name_table.rda differ diff --git a/man/add_id_for_municipality.Rd b/man/add_id_for_municipality.Rd new file mode 100644 index 0000000..5903e5d --- /dev/null +++ b/man/add_id_for_municipality.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/id-for-municipality.R +\name{add_id_for_municipality} +\alias{add_id_for_municipality} +\title{Add an unique identifier for municipality (id_muni) column to a dataset} +\usage{ +add_id_for_municipality( + dataset, + id_variable_name = "id_muni", + muni_table = anexo_municipios, + overwrite = FALSE +) +} +\arguments{ +\item{dataset}{A dataframe with a column containing the name of the +municipality, named `municipality`.} + +\item{id_variable_name}{The name of the new column to be created. Default is +`id_muni`.} + +\item{muni_table}{A data frame containing the municipality list table.} + +\item{overwrite}{A boolean indicating whether to overwrite an existing +column.} +} +\value{ +A dataframe with a new column `id_muni` containing the unique + identifier for the municipality, placed before the `municipality` column. +} +\description{ +Add an unique identifier for municipality (id_muni) column to a dataset +} +\examples{ +id_for_municipality("Villa Ricardo Mugia - Icla", muni_list_table = muni_gb2014_lookup) +id_for_municipality_2("Villa Ricardo Mugia - Icla", "Chuquisaca") +} diff --git a/man/add_muni_gb2014_from_id.Rd b/man/add_muni_gb2014_from_id.Rd new file mode 100644 index 0000000..890a04d --- /dev/null +++ b/man/add_muni_gb2014_from_id.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/id-for-municipality.R +\name{add_muni_gb2014_from_id} +\alias{add_muni_gb2014_from_id} +\title{Add Municipality GB2014 Code from ID} +\usage{ +add_muni_gb2014_from_id(dataframe, muni_table = muni_id_lookup_table) +} +\arguments{ +\item{dataframe}{A dataframe containing a column `id_muni`.} + +\item{muni_table}{A lookup table containing `id_muni` and `muni_gb2014` columns.} +} +\value{ +A dataframe with an additional column `muni_gb2014` containing the corresponding GB2014 codes. +} +\description{ +This function adds a column `muni_gb2014` to a dataframe based on the `id_muni` column. +It uses a lookup table to map `id_muni` to `muni_gb2014`. +} diff --git a/man/anexo_municipios.Rd b/man/anexo_municipios.Rd new file mode 100644 index 0000000..a7bcbe0 --- /dev/null +++ b/man/anexo_municipios.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/aaa-data.R +\docType{data} +\name{anexo_municipios} +\alias{anexo_municipios} +\title{Municipal Reference Table for Bolivia} +\format{ +A data frame with columns: +\describe{ + \item{codigo_ine}{Municipal code (INE code)} + \item{municipio}{Municipality name} + \item{provincia}{Province name} + \item{departamento}{Department name} + \item{superficie}{Area of the municipality in square kilometers} + \item{poblacion}{Population of the municipality} + \item{densidad}{Population density (people per square kilometer)} + \item{municipio_alt}{Alternative municipality name} +} +} +\usage{ +anexo_municipios +} +\description{ +A reference table containing municipality information for Bolivia, +read from the official anexo of municipalities. +} +\details{ +This table contains the official list of municipalities in Bolivia +with their corresponding INE (Instituto Nacional de Estadística) codes, +provinces, and departments. The table is used for municipality lookup +and ID assignment functions. +} +\keyword{datasets} diff --git a/man/id_for_municipality.Rd b/man/id_for_municipality.Rd new file mode 100644 index 0000000..8a5f76f --- /dev/null +++ b/man/id_for_municipality.Rd @@ -0,0 +1,60 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/id-for-municipality.R +\name{id_for_municipality} +\alias{id_for_municipality} +\alias{id_for_municipality_2} +\title{Get the ID of a municipality based on its name} +\usage{ +id_for_municipality( + municipality_i, + department_i = "", + muni_list_table = anexo_municipios +) + +id_for_municipality_2( + municipality_i, + department_i = "", + muni_list_table = muni_id_lookup_table +) +} +\arguments{ +\item{municipality_i}{Name of the municipality to search for.} + +\item{department_i}{Optional name of the department to filter by.} + +\item{muni_list_table}{A data frame containing the municipality list table.} +} +\value{ +The unique identifier for the municipality as listed in the `code` = + column of the `muni_list_table`. If no match is found, returns an empty string. +} +\description{ +Returns the unique identifier for a municipality based on its name and +optionally the department. These identifiers are the codes used +in the Bolivian government databases, known as INE codes for the +Instituto Nacional de Estadística. +} +\details{ +The function will warn the user if a municipality name without +department corresponds to multiple municipalities. In this case, the first +municipality found will be returned. (There are about a dozen such cases in Bolivia.) + +`id_for_municipality_2` is a version of the function that handles lookup + tables with lists of municipalities. It is relies on `muni_id_lookup_table`. +} +\examples{ + id_for_municipality("La Paz") # 020101 + id_for_municipality("La Paz", "La Paz") # 020101 + id_for_municipality("La Paz", "Cochabamba") # empty string + id_for_municipality("Potosí", "Potosí") # 050101 + id_for_municipality("Potosi") # 050101 + id_for_municipality("San Ramón") # 071103 + id_for_municipality("San Ramón", "Santa Cruz") # 071103 + id_for_municipality("San Ramón", "Beni") # 080702 + + id_for_municipality_2("zudanez") # "010301" + id_for_municipality_2("villa montes") # "060303" + id_for_municipality_2("villamontes") # "060303" + id_for_municipality_2("San Ignacio") # "070301" + id_for_municipality_2("San Ignacio", "Beni") # "080501" +} diff --git a/man/muni_gb2014_conversion.Rd b/man/muni_gb2014_conversion.Rd new file mode 100644 index 0000000..df85d0c --- /dev/null +++ b/man/muni_gb2014_conversion.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/aaa-data.R +\docType{data} +\name{muni_gb2014_conversion} +\alias{muni_gb2014_conversion} +\title{Municipality Name Conversion Table for GB2014 Format} +\format{ +A data frame with columns: +\describe{ + \item{id_muni}{Municipal ID (INE code)} + \item{muni_gb2014}{Municipality name in GB2014 format} + \item{muni_anexo}{Standard municipality name from anexo} + \item{recode}{R code string for recoding municipality names} +} +} +\usage{ +muni_gb2014_conversion +} +\description{ +A lookup table for converting between standard municipality names +and GB2014 format municipality names in Bolivia. +} +\details{ +This table contains only municipalities where the GB2014 format name +differs from the standard anexo name. It includes generated R code +strings that can be used to perform the name conversions automatically. +} +\keyword{datasets} diff --git a/man/muni_gb2014_lookup.Rd b/man/muni_gb2014_lookup.Rd new file mode 100644 index 0000000..69e9ad9 --- /dev/null +++ b/man/muni_gb2014_lookup.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/aaa-data.R +\docType{data} +\name{muni_gb2014_lookup} +\alias{muni_gb2014_lookup} +\title{Municipality Lookup Table in GB2014 Format} +\format{ +A data frame with columns: +\describe{ + \item{id_muni}{Municipal ID (INE code)} + \item{municipality}{Municipality name in GB2014 format} + \item{department}{Department name} +} +} +\usage{ +muni_gb2014_lookup +} +\description{ +A simplified lookup table using GB2014 format municipality names +for municipality ID and department lookup. +} +\details{ +This table provides a streamlined lookup mechanism using GB2014 +format municipality names. It's derived from muni_id_lookup_table +and can be used when working with data that uses GB2014 naming +conventions. +} +\keyword{datasets} diff --git a/man/muni_id_lookup_table.Rd b/man/muni_id_lookup_table.Rd new file mode 100644 index 0000000..5b7fb0c --- /dev/null +++ b/man/muni_id_lookup_table.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/aaa-data.R +\docType{data} +\name{muni_id_lookup_table} +\alias{muni_id_lookup_table} +\title{Municipality ID Lookup Table} +\format{ +A data frame with columns: +\describe{ + \item{id_muni}{Municipal ID (INE code)} + \item{muni_gb2014}{Municipality name in GB2014 map format} + \item{muni_anexo}{Standard municipality name from anexo} + \item{muni_ine}{Municipality name in INE table} + \item{muni_census}{Municipality name in census table} + \item{department}{Department name} + \item{muni_list}{List of alternative municipality names} + \item{n_unique}{Number of unique names for the municipality} +} +} +\usage{ +muni_id_lookup_table +} +\description{ +A comprehensive lookup table for municipality IDs and names in Bolivia, +including alternative municipality names and mappings. +} +\details{ +This table provides comprehensive municipality lookup functionality, +including alternative spellings and name variations. It supports +the id_for_municipality_2() function and other advanced municipality +matching operations. +} +\keyword{datasets} diff --git a/man/municipality_vector_from_id.Rd b/man/municipality_vector_from_id.Rd new file mode 100644 index 0000000..dda97ce --- /dev/null +++ b/man/municipality_vector_from_id.Rd @@ -0,0 +1,74 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/id-for-municipality.R +\name{municipality_vector_from_id} +\alias{municipality_vector_from_id} +\alias{municipality_name_from_id} +\alias{province_name_from_id} +\alias{department_name_from_id} +\title{Get Municipality Details from ID Vector} +\usage{ +municipality_vector_from_id(id_muni, muni_list_table = anexo_municipios) + +municipality_name_from_id(id_muni, muni_list_table = anexo_municipios) + +province_name_from_id(id_muni, muni_list_table = anexo_municipios) + +department_name_from_id(id_muni, muni_list_table = anexo_municipios) +} +\arguments{ +\item{id_muni}{A character vector of municipality IDs to look up.} + +\item{muni_list_table}{A lookup dataframe containing municipality information.} +} +\value{ +A list with three components: + - `municipality`: A character vector of municipality names. + - `province`: A character vector of province names. + - `department`: A character vector of department names. + +A string with the name of the municipality, province, or department. +} +\description{ +This function looks up municipality details (municipality, province, and department) +from a list of municipality IDs in the given `muni_list_table`. +} +\details{ +The function performs a lookup by matching the `id_muni` with the `cod.mun` column in `muni_list_table`. +If an ID is not found, the corresponding entries in the output list will be `NA`. +} +\examples{ +# Example `muni_list_table` +anexo_municipios <- tibble::tibble( + cod.mun = c("010101", "010102", "010103"), + municipality = c("Sucre", "Yotala", "Poroma"), + province = c("Oropeza", "Oropeza", "Oropeza"), + department = c("Chuquisaca", "Chuquisaca", "Chuquisaca") +) + +# Lookup municipality details +ids <- c("010101", "010102", "010999") +result <- municipality_name_from_id(ids, anexo_municipios) +print(result) + +municipality_name_from_id("020101") # "La Paz" +municipality_name_from_id("050101") # "Potosí" +municipality_name_from_id("071103") # "San Ramón" +municipality_name_from_id("080702") # "San Ramón" +municipality_name_from_id("010301") # "Villa Zudáñez" +province_name_from_id("020101") # "Murillo" +province_name_from_id("050101") # "Tomás Frías" +province_name_from_id("071103") # "Ñuflo de Chaves" +province_name_from_id("080702") # "Mamoré" +province_name_from_id("010301") # "Zudáñez" +province_name_from_id("060303") # "Gran Chaco" +province_name_from_id("070301") # "Velasco" +province_name_from_id("080501") # "Moxos" +department_name_from_id("020101") # "La Paz" +department_name_from_id("050101") # "Potosí" +department_name_from_id("071103") # "Santa Cruz" +department_name_from_id("080702") # "Beni" +department_name_from_id("010301") # "Chuquisaca" +department_name_from_id("060303") # "Tarija" +department_name_from_id("070301") # "Santa Cruz" +department_name_from_id("080501") # "Beni" +} diff --git a/man/rename_anexo_columns.Rd b/man/rename_anexo_columns.Rd new file mode 100644 index 0000000..a3411cf --- /dev/null +++ b/man/rename_anexo_columns.Rd @@ -0,0 +1,39 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/id-for-municipality.R +\name{rename_anexo_columns} +\alias{rename_anexo_columns} +\title{Rename Columns in a Dataframe for Standardization} +\usage{ +rename_anexo_columns(dataframe) +} +\arguments{ +\item{dataframe}{A dataframe containing the columns to be standardized.} +} +\value{ +A dataframe with standardized column names. +} +\description{ +This function renames specific columns in a dataframe to standardize their names. +It ensures that the columns `municipality`, `province`, and `department` are present, renaming existing columns +(`municipio`, `provincia`, `departamento`) if necessary. Additionally, it renames any column starting with `codigo` +to `cod.mun`. +} +\details{ +- The function checks for the presence of `municipality`, `province`, and `department`. + If these columns are missing, it renames `municipio`, `provincia`, and `departamento` to their English equivalents. +- Any column name starting with `codigo` is renamed to `cod.mun`. +} +\examples{ +# Example dataframe +df <- data.frame( + municipio = c("La Paz", "Cochabamba"), + provincia = c("Pedro Domingo Murillo", "Cercado"), + departamento = c("La Paz", "Cochabamba"), + codigo123 = c(101, 102) +) + +# Rename columns +renamed_df <- rename_anexo_columns(df) +print(names(renamed_df)) # Should include "municipality", "province", "department", "cod.mun" + +} diff --git a/man/render_presidency.Rd b/man/render_presidency.Rd index 7730bc8..d3b5874 100644 --- a/man/render_presidency.Rd +++ b/man/render_presidency.Rd @@ -35,4 +35,7 @@ render_presidency("Gonzalo Sanchez de Lozada (2nd)", "first_day") # "2002-08-06" render_presidency("Gonzalo Sanchez de Lozada (2nd)", "id_presidency") # "p107" render_presidency("Gonzalo Sanchez de Lozada (2nd)", "presidency_surnames") render_presidency("Gonzalo Sanchez de Lozada (2nd)", "presidency_year_es") +render_presidency("p111", "presidency_commonname_es", source_var="id_presidency") +render_presidency("p111", "presidency_initials_num", source_var="id_presidency") +render_presidency("p111", "presidency_commonname", source_var="id_presidency") } diff --git a/man/str_equivalent_list.Rd b/man/str_equivalent_list.Rd new file mode 100644 index 0000000..9200bc5 --- /dev/null +++ b/man/str_equivalent_list.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/str-equivalent.R +\name{str_equivalent_list} +\alias{str_equivalent_list} +\title{Check for Equivalent Strings in a List} +\usage{ +str_equivalent_list(string, string_list) +} +\arguments{ +\item{string}{A character string to compare.} + +\item{string_list}{A character vector of potential matches.} +} +\value{ +A logical value: \code{TRUE} if any string in the list is equivalent to the target string, +or \code{FALSE} otherwise. +} +\description{ +This function checks if any string in a list is equivalent to a target string, +considering case insensitivity, accent removal, whitespace trimming, and non-breaking space replacement. +} +\details{ +The function uses \code{\link{str_equivalent}} to determine string equivalence and applies it +to each element of the list. +} +\examples{ +str_equivalent_list("café", c("cafe", "tea", "coffee")) # Returns TRUE +str_equivalent_list("hello", c("world", "hi")) # Returns FALSE + +} +\seealso{ +\code{\link{str_equivalent}} +} diff --git a/tests/testthat/test-add-presidency-column.R b/tests/testthat/test-add-presidency-column.R index 1b0e938..7d2e97a 100644 --- a/tests/testthat/test-add-presidency-column.R +++ b/tests/testthat/test-add-presidency-column.R @@ -28,4 +28,8 @@ test_that("Testing render_presidency", { expect_equal(render_presidency("Gonzalo Sanchez de Lozada (2nd)", "id_presidency"), "p107") expect_equal(render_presidency("Gonzalo Sanchez de Lozada (2nd)", "presidency_surnames"), "Sánchez de Lozada") expect_equal(render_presidency("Gonzalo Sanchez de Lozada (2nd)", "presidency_year_es"), "Gonzalo Sánchez de Lozada (2002-2003)") + + expect_equal(render_presidency("p111", "presidency_commonname_es", source_var="id_presidency"), "Gobierno interino militar (2019)") + expect_equal(render_presidency("p111", "presidency_initials_num", source_var="id_presidency"), "Mil") + expect_equal(render_presidency("p111", "presidency_commonname", source_var="id_presidency"), "Military government (2019)") }) diff --git a/tests/testthat/test-id-for-municipality.R b/tests/testthat/test-id-for-municipality.R new file mode 100644 index 0000000..9f1ca66 --- /dev/null +++ b/tests/testthat/test-id-for-municipality.R @@ -0,0 +1,218 @@ +test_that("Testing id_for_municipality", { + expect_equal(id_for_municipality("La Paz"), "020101") + expect_equal(id_for_municipality("La Paz", "La Paz"), "020101") + expect_warning(id_for_municipality("La Paz", "Cochabamba"), "Municipality La Paz not found in department Cochabamba . Returning empty string.") + expect_equal(suppressWarnings(id_for_municipality("La Paz", "Cochabamba")), "") + expect_equal(id_for_municipality("Potosí", "Potosí"), "050101") + expect_equal(id_for_municipality("Potosi"), "050101") + expect_equal(suppressWarnings(id_for_municipality("San Ramón")), "071103") + expect_warning(id_for_municipality("San Ramon"), "Multiple municipalities found for San Ramon without department . Returning first ID found.") + expect_equal(id_for_municipality("San Ramón", "Santa Cruz"), "071103") + expect_equal(id_for_municipality("San Ramón", "Beni"), "080702") +}) + +test_that("Testing id_for_municipality_2", { + expect_equal(id_for_municipality_2("zudanez"), "010301") + expect_equal(id_for_municipality_2("villa montes"), "060303") + expect_equal(id_for_municipality_2("villamontes"), "060303") + expect_warning(id_for_municipality_2("San Ignacio"), "Multiple municipalities found for San Ignacio without department . Returning first ID found.") + expect_equal(suppressWarnings(id_for_municipality_2("San Ignacio")), "070301") + expect_equal(id_for_municipality_2("San Ignacio", "Beni"), "080501") +}) + +test_that("Testing municipality_vector_from_id", { + expect_equal(municipality_vector_from_id("020101"), list(municipality = "La Paz", province = "Murillo", department = "La Paz")) + expect_equal(municipality_vector_from_id("050101") %>% unlist %>% unname, c("Potosí", "Tomás Frías", "Potosí")) + expect_equal(municipality_vector_from_id("071103") %>% unlist %>% unname, c("San Ramón", "Ñuflo de Chaves", "Santa Cruz")) + expect_equal(municipality_vector_from_id("080702") %>% unlist %>% unname, c("San Ramón", "Mamoré", "Beni")) + expect_equal(municipality_vector_from_id("010301") %>% unlist %>% unname, c("Villa Zudáñez", "Zudáñez", "Chuquisaca")) + expect_equal(municipality_vector_from_id("060303") %>% unlist %>% unname, c("Villa Montes", "Gran Chaco", "Tarija")) + expect_equal(municipality_vector_from_id("070301") %>% unlist %>% unname, c("San Ignacio de Velasco", "Velasco", "Santa Cruz")) + expect_equal(municipality_vector_from_id("080501") %>% unlist %>% unname, c("San Ignacio de Moxos", "Moxos", "Beni")) +}) + +test_that("Testing municipality_name_from_id", { + expect_equal(municipality_name_from_id("020101"), "La Paz") + expect_equal(municipality_name_from_id("050101"), "Potosí") + expect_equal(municipality_name_from_id("071103"), "San Ramón") + expect_equal(municipality_name_from_id("080702"), "San Ramón") + expect_equal(municipality_name_from_id("010301"), "Villa Zudáñez") + expect_equal(municipality_name_from_id("060303"), "Villa Montes") + expect_equal(municipality_name_from_id("070301"), "San Ignacio de Velasco") + expect_equal(municipality_name_from_id("080501"), "San Ignacio de Moxos") +}) + +test_that("Testing province_name_from_id", { + expect_equal(province_name_from_id("020101"), "Murillo") + expect_equal(province_name_from_id("050101"), "Tomás Frías") + expect_equal(province_name_from_id("071103"), "Ñuflo de Chaves") + expect_equal(province_name_from_id("080702"), "Mamoré") + expect_equal(province_name_from_id("010301"), "Zudáñez") + expect_equal(province_name_from_id("060303"), "Gran Chaco") + expect_equal(province_name_from_id("070301"), "Velasco") + expect_equal(province_name_from_id("080501"), "Moxos") +}) + +test_that("Testing department_name_from_id", { + expect_equal(department_name_from_id("020101"), "La Paz") + expect_equal(department_name_from_id("050101"), "Potosí") + expect_equal(department_name_from_id("071103"), "Santa Cruz") + expect_equal(department_name_from_id("080702"), "Beni") + expect_equal(department_name_from_id("010301"), "Chuquisaca") + expect_equal(department_name_from_id("060303"), "Tarija") + expect_equal(department_name_from_id("070301"), "Santa Cruz") + expect_equal(department_name_from_id("080501"), "Beni") +}) + +# Create test data based on the provided structure +test_anexo_municipios <- data.frame( + codigo = c("010101", "010102", "010103", "010201", "010202", "010301", "010302", "010303"), + municipio = c("Sucre", "Yotala", "Poroma", "Azurduy", "Tarvita", "Villa Zudáñez", "Presto", "Villa Mojocoya"), + provincia = c("Oropeza", "Oropeza", "Oropeza", "Azurduy", "Azurduy", "Zudáñez", "Zudáñez", "Zudáñez"), + departamento = c("Chuquisaca", "Chuquisaca", "Chuquisaca", "Chuquisaca", "Chuquisaca", "Chuquisaca", "Chuquisaca", "Chuquisaca"), + stringsAsFactors = FALSE +) + +test_muni_id_lookup_table <- data.frame( + id_muni = c("010101", "010102", "010103", "010201", "010202", "010301", "010302", "010303"), + muni_gb2014 = c("Sucre", "Yotala", "Poroma", "Azurduy", "Tarvita", "Zudañez", "Presto", "Mojocoya"), + muni_anexo = c("Sucre", "Yotala", "Poroma", "Azurduy", "Tarvita", "Villa Zudáñez", "Presto", "Villa Mojocoya"), + department = c("Chuquisaca", "Chuquisaca", "Chuquisaca", "Chuquisaca", "Chuquisaca", "Chuquisaca", "Chuquisaca", "Chuquisaca"), + muni_list = I(list( + "Sucre", + "Yotala", + "Poroma", + "Azurduy", + "Tarvita", + c("Villa Zudáñez", "Zudañez"), + "Presto", + c("Villa Mojocoya", "Mojocoya") + )), + stringsAsFactors = FALSE +) + +test_that("id_for_municipality returns correct IDs for exact matches", { + expect_equal(id_for_municipality("Sucre", "", test_anexo_municipios), "010101") + expect_equal(id_for_municipality("Yotala", "", test_anexo_municipios), "010102") + expect_equal(id_for_municipality("Villa Zudáñez", "", test_anexo_municipios), "010301") +}) + +test_that("id_for_municipality handles department filtering correctly", { + expect_equal(id_for_municipality("Sucre", "Chuquisaca", test_anexo_municipios), "010101") + expect_warning( + result <- id_for_municipality("Sucre", "La Paz", test_anexo_municipios), + "Municipality Sucre not found in department La Paz" + ) + expect_equal(result, "") +}) + +test_that("id_for_municipality handles non-existent municipalities", { + expect_warning( + result <- id_for_municipality("NonExistent", "", test_anexo_municipios), + "Municipality NonExistent not found" + ) + expect_equal(result, "") +}) + +test_that("id_for_municipality handles NA input", { + result <- id_for_municipality(NA, "", test_anexo_municipios) + expect_true(is.na(result)) +}) + +test_that("id_for_municipality_2 returns correct IDs for exact matches", { + expect_equal(id_for_municipality_2("Sucre", "", test_muni_id_lookup_table), "010101") + expect_equal(id_for_municipality_2("Yotala", "", test_muni_id_lookup_table), "010102") + expect_equal(id_for_municipality_2("Villa Zudáñez", "", test_muni_id_lookup_table), "010301") +}) + +test_that("id_for_municipality_2 handles alternative names correctly", { + expect_equal(id_for_municipality_2("Zudañez", "", test_muni_id_lookup_table), "010301") + expect_equal(id_for_municipality_2("Villa Zudáñez", "", test_muni_id_lookup_table), "010301") + expect_equal(id_for_municipality_2("Mojocoya", "", test_muni_id_lookup_table), "010303") + expect_equal(id_for_municipality_2("Villa Mojocoya", "", test_muni_id_lookup_table), "010303") +}) + +test_that("id_for_municipality_2 handles department filtering correctly", { + expect_equal(id_for_municipality_2("Sucre", "Chuquisaca", test_muni_id_lookup_table), "010101") + expect_warning( + result <- id_for_municipality_2("Sucre", "La Paz", test_muni_id_lookup_table), + "Municipality Sucre not found in department La Paz" + ) + expect_equal(result, "") +}) + +test_that("id_for_municipality_2 handles non-existent municipalities", { + expect_warning( + result <- id_for_municipality_2("NonExistent", "", test_muni_id_lookup_table), + "Municipality NonExistent not found" + ) + expect_equal(result, "") +}) + +test_that("id_for_municipality_2 handles NA input", { + result <- id_for_municipality_2(NA, "", test_muni_id_lookup_table) + expect_true(is.na(result)) +}) + +test_that("id_for_municipality_2 validates input parameters", { + expect_error( + id_for_municipality_2(123, "", test_muni_id_lookup_table), + "municipality_i is not a character vector" + ) + expect_error( + id_for_municipality_2("Sucre", 123, test_muni_id_lookup_table), + "department_i is not a character vector" + ) + expect_error( + id_for_municipality_2("Sucre", "", "not_a_dataframe"), + "muni_list_table is not a data frame" + ) + + # # Test missing muni_list column + # bad_table <- test_muni_id_lookup_table + # bad_table$muni_list <- NULL + # expect_error( + # id_for_municipality_2("Sucre", "", bad_table), + # "muni_list not in names\\(muni_list_table\\)" + # ) +}) + +test_that("Functions handle numeric codes correctly", { + # Test with numeric codes in anexo table + numeric_anexo <- test_anexo_municipios + numeric_anexo$codigo <- as.numeric(c(10101, 10102, 10103, 10201, 10202, 10301, 10302, 10303)) + + expect_equal(id_for_municipality("Sucre", "", numeric_anexo), "010101") + expect_equal(id_for_municipality("Yotala", "", numeric_anexo), "010102") +}) + +test_that("Functions handle case-insensitive matching through str_equivalent", { + # Assuming str_equivalent handles case-insensitive matching + # These tests may need adjustment based on actual str_equivalent behavior + skip_if_not(exists("str_equivalent"), "str_equivalent function not available") + + expect_equal(id_for_municipality("sucre", "", test_anexo_municipios), "010101") + expect_equal(id_for_municipality_2("sucre", "", test_muni_id_lookup_table), "010101") +}) + +test_that("Multiple municipality warning is generated when appropriate", { + # Create test data with duplicate municipality names in different departments + multi_muni_table <- rbind( + test_anexo_municipios, + data.frame( + codigo = "020101", + municipio = "Sucre", # Duplicate name + provincia = "Murillo", + departamento = "La Paz", + stringsAsFactors = FALSE + ) + ) + + expect_warning( + result <- id_for_municipality("Sucre", "", multi_muni_table), + "Multiple municipalities found for Sucre.*Returning first ID found" + ) + expect_equal(result, "010101") # Should return first match +}) + +