ISO 2 letter country code in r
# Load necessary library
library(countrycode)
# Create a vector of country names
countries <- c("United States", "Canada", "United Kingdom")
# Use countrycode package to convert country names to ISO 2 letter country codes
iso_codes <- countrycode(sourcevar = countries, origin = "country.name", destination = "iso2c")
print(iso_codes)
Load the
countrycode
library, which provides functions to convert between different country coding systems.Create a vector named
countries
containing the names of countries for which you want to find ISO 2 letter country codes.Utilize the
countrycode
function, specifying thesourcevar
as the vector of country names (countries
), setting theorigin
as"country.name"
to indicate the input type as country names, and thedestination
as"iso2c"
to specify the desired output as ISO 2 letter country codes.Display the resulting ISO 2 letter country codes using the
print
function.