convert country code to country name in r

To convert a country code to a country name in R, you can use the countrycode package. This package provides functionalities to convert country codes to country names and vice versa. Here are the steps to do it:

  1. Install and load the countrycode package:
install.packages("countrycode")
library(countrycode)
  1. Define a vector of country codes that you want to convert. For example, let's say you have the country codes US, GB, and FR:
country_codes <- c("US", "GB", "FR")
  1. Use the countrycode() function to convert the country codes to country names. Specify the input country codes, the origin country code type (e.g., iso2c for two-letter codes), and the destination country name type (e.g., country.name):
country_names <- countrycode(country_codes, "iso2c", "country.name")

After executing these steps, the country_names vector will contain the corresponding country names for the input country codes.

Note: To convert country names to country codes, you can use the same countrycode() function by specifying the input country names and the destination country code type.

Here's the complete code:

install.packages("countrycode")
library(countrycode)

country_codes <- c("US", "GB", "FR")
country_names <- countrycode(country_codes, "iso2c", "country.name")

country_names

This will output:

[1] "United States" "United Kingdom" "France"

I hope this helps! Let me know if you have any further questions.