Skip to contents

Easily convert a country name to its ISO code and vice versa. Official United Nations country names are used. See countrynames for full dictionary.

Usage

countryname(key, from = "iso3", to = "name", exact = FALSE, quiet = FALSE)

Arguments

key

(Case-insensitive) name or ISO code of country.

from

Current type of key.

to

Target type of key.

exact

Whether to use exact matching.

quiet

Turn off warning messages.

Value

String or vector of strings.

Details

Under the default exact = FALSE, matching is done using stringr::str_detect, which means keys need not be exact. For example, countryname("iran", from = "name", to = "iso3) will return the correct ISO code "IRN" even if the dictionary name is "Islamic Republic of Iran". One can therefore check a country's official name by running countryname("iran", from = "name").

If there are multiple matches, countryname() will only return the first match. Thus, while "guinea" matches with four countries (Equatorial Guinea, Guinea, Guinea-Bissau, Papua New Guinea), countryname("guinea") will only return the keys for Equatorial Guinea, which is a problem if one wants to obtain the keys for Guinea. Set exact = TRUE to use exact matching.

Regardless of how exact is set, matching remains case-insensitive,

Examples

# Default export a series of country code to country name
countryname(c("deu", "phl"))
#> [1] "Germany"     "Philippines"

# Country name to Country code
countryname("germany", from = "name", to = "iso3")
#> [1] "DEU"

# Country name to IOM region that matches the specific color palette pal_region
countryname("germany", from = "name", to = "iom.region")
#> [1] "European Economic Area"

# testing multiple match and  exact = TRUE
countryname("guinea", from = "name")
#> Warning: The following had multiple matches and only the first was returned. For more
#> control over matching, set `exact = TRUE`.
#>  "guinea" = "Equatorial Guinea"
#> [1] "Equatorial Guinea"

countryname("guinea", from = "name", exact = TRUE)
#> [1] "Guinea"

# names to full names
countryname("united kingdom", from = "name")
#> [1] "United Kingdom of Great Britain and Northern Ireland"