two letter country code in r
To obtain the two-letter country codes in R, you can use the countrycode
package. The countrycode
package provides a simple and efficient way to convert country names to country codes and vice versa.
Here are the steps to obtain the two-letter country codes in R:
- Install the
countrycode
package by running the following code:
install.packages("countrycode")
- Load the package into your R session using the
library
function:
library(countrycode)
- Use the
countrycode
function to convert country names to two-letter country codes. The function takes three arguments:sourcevar
(the variable containing the country names),origin
(the origin country code), anddestination
(the destination country code). In this case, since we want to convert country names to two-letter country codes, we'll setorigin
to"country.name"
anddestination
to"iso2c"
. Here's an example:
country_names <- c("United States", "United Kingdom", "Canada")
country_codes <- countrycode(sourcevar = country_names, origin = "country.name", destination = "iso2c")
- The
countrycode
function will return a vector of two-letter country codes corresponding to the input country names. In the example above,country_codes
will contain the values"US"
,"GB"
, and"CA"
.
That's it! You now have the two-letter country codes for the specified country names using R and the countrycode
package.