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)
  1. Load the countrycode library, which provides functions to convert between different country coding systems.

  2. Create a vector named countries containing the names of countries for which you want to find ISO 2 letter country codes.

  3. Utilize the countrycode function, specifying the sourcevar as the vector of country names (countries), setting the origin as "country.name" to indicate the input type as country names, and the destination as "iso2c" to specify the desired output as ISO 2 letter country codes.

  4. Display the resulting ISO 2 letter country codes using the print function.