Skip to contents

names_to_iso3() takes in a vector of country names and returns a vector of ISO3 codes.

Usage

names_to_iso3(
  names,
  language = c("en", "es", "ru", "ar", "zh", "fr"),
  ignore_case = T,
  fuzzy_matching = c("yes", "no", "user_input"),
  method = "jw",
  p = 0.1
)

Arguments

names

A character vector of country names.

language

A character value specifying the language of the country names. Should be specified using the ISO2 language code. Defaults to "en", but matching available for all 6 official WHO languages. Possible values are "en", "es", "ar", "fr", "ru", and "zh".

ignore_case

A logical value specifying whether or not to ignore case when string matching. Defaults to TRUE.

fuzzy_matching

A character value specifying whether or not to do fuzzy matching, or to require user input when no exact match is found. Defaults to "yes".

method

A character value specifying the distance method for use in fuzzy matching if an exact match is not found. Defaults to Jaro-Winker due to its value in matching names. See stringdist::stringdist() for all possible methods.

p

A numeric value between 0 and 0.25 specifying the penalty factor for Jaro-Winkler distance. Ignored if method is not "jw".

Value

A vector of country codes matched to the names.

Details

The function first searches for exact matches in the countries data frame, and if none are found, uses fuzzy matching to guess the country. Matching is done using stringdist fuzzy matching methods, although names_to_codes() allows the user to not do any fuzzy matching (non-exact matches returned as NA) or require user input.