Skip to contents

Provide custom countrycode function

Usage

gtl_admin_code(..., country = c("China", "Switzerland", "Russia"))

Arguments

...

Arguments passed on to countrycode::countrycode

sourcevar

Vector which contains the codes or country names to be converted (character or factor)

origin

A string which identifies the coding scheme of origin (e.g., "iso3c"). See codelist for a list of available codes.

destination

A string or vector of strings which identify the coding scheme of destination (e.g., "iso3c" or c("cowc", "iso3c")). See codelist for a list of available codes. When users supply a vector of destination codes, they are used sequentially to fill in missing values not covered by the previous destination code in the vector.

warn

Prints unique elements from sourcevar for which no match was found

nomatch

When countrycode fails to find a match for the code of origin, it fills-in the destination vector with nomatch. The default behavior is to fill non-matching codes with NA. If nomatch = NULL, countrycode tries to use the origin vector to fill-in missing values in the destination vector. nomatch must be either NULL, of length 1, or of the same length as sourcevar.

custom_dict

A data frame which supplies a new dictionary to replace the built-in country code dictionary. Each column contains a different code and must include no duplicates. The data frame format should resemble codelist. Users can pre-assign attributes to this custom dictionary to affect behavior (see Examples section):

  • "origin.regex" attribute: a character vector with the names of columns containing regular expressions.

  • "origin.valid" attribute: a character vector with the names of columns that are accepted as valid origin codes.

custom_match

A named vector which supplies custom origin and destination matches that will supercede any matching default result. The name of each element will be used as the origin code, and the value of each element will be used as the destination code.

origin_regex

NULL or Logical: When using a custom dictionary, if TRUE then the origin codes will be matched as regex, if FALSE they will be matched exactly. When NULL, countrycode will behave as TRUE if the origin name is in the custom_dictionary's origin_regex attribute, and FALSE otherwise. See examples section below.

country

Select the custom dictionary, currently one of China, Switzerland or Russia

Value

A vector with the desired output

Examples

tibble::tribble(
  ~canton, ~population,
  "Neuchâtel", 176496,
  "Vaud", 805098,
  "Genève", 504128
) -> population

population |>
  dplyr::mutate(iso = gtl_admin_code(
    sourcevar = canton,
    origin = "canton.name.regex",
    destination = "iso",
    origin_regex = TRUE,
    country = "Switzerland"
  ))
#> # A tibble: 3 × 3
#>   canton    population iso  
#>   <chr>          <dbl> <chr>
#> 1 Neuchâtel     176496 CH-NE
#> 2 Vaud          805098 CH-VD
#> 3 Genève        504128 CH-GE