knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/",
  out.width = "100%"
)
library(countries)

countries

countries is an R package designed to quickly wrangle, merge and explore country data. This package contains functions to easily identify and convert country names, pull country info and datasets, merge country data from different sources, and easily make world maps.

Installing and loading the package

The package can be installed from CRAN.

# Install package from CRAN
install.packages("countries")

# load package
library(countries)

Alternatively, the development version can be downloaded directly from the Github repository. This can be done with the devtools package.

# Install and load devtools
install.packages("devtools")
library(devtools)

# Install countries
devtools::install_github("fbellelli/countries", build_vignettes = TRUE)

# load package
library(countries)

Dealing with country names

The package contains several functions to work with country names. For instance, the function country_name() can be used to convert country names to different naming conventions or to translate them to different languages. country_name() can identify countries even when they are provided in mixed formats or in different languages. It is robust to small misspellings and recognises many alternative country names and old nomenclatures. Learn more about how to deal with country names in this article.

example <- c("US","C@ète d^Ivoire", "Morocco","FYROM", "Arabie Saoudite")

# Getting 3-letters ISO code
country_name(x= example, to="ISO3")

# Translating to spanish
country_name(x= example, to="name_es")

# Getting multiple nomenclatures
country_name(x= example, to=c("ISO3","ISO2","UN_en"))

The function is_country() can be used to test for country names or subsets of countries:

#Detect strings that are country names
is_country(x = c("ITA","Estados Unidos","bungalow","dog",542))

#Checking for a specific subset of countries
is_country(x = c("Ceylon","LKA","Indonesia","Inde"), check_for = c("India","Sri Lanka"))

The functions list_countries() and random_countries() allow to get a list of country names. The former will return a list of ALL countries, while the second provides n randomly picked countries.

# Get 5 random country names in different languages/nomenclatures
random_countries(5)
random_countries(5, nomenclature = "ISO3")
random_countries(5, nomenclature = "name_ar")

Getting information about countries

country_info() allows to download a variety of information about countries from REST Countries API, such as: currencies used, capital city, language spoken, flag, neighbouring countries, and much more. You can find more information about this function in the documentation.

# What are the official languages of Switzerland?
country_info("Switzerland", "languages")

# Get information on the capital name and currencies for multiple countries
country_info(c("Canada", "Mozambique", "India"), c("capital", "currencies"))

Easy country maps

With quick_map(), it takes only one line of code to produce chloropleth maps. It automatically recognises country names in multiple languages and nomenclatures. This allows to produce publication-grade maps in seconds. Moreover, the output is a ggplot object, so the visual look can be customised in infinite ways. You can find more examples in this article.

# downloading some sample data to plot
example_data <- country_info(fields = c("car"))

# make a map
quick_map(example_data, plot_col = "car.side")

Merging country data

The function auto_merge() simplifies the merging of country data tables by: 1) allowing merging of 2+ tables at the same time, 2) Supporting automatic detection of columns to merge, 3) automatically handling different country naming conventions and date formats, 4) automatic pivoting of country names or years in tables' headers. Learn more about country names functions in this article.

# Let's create 4 tables with different formats and country names
tab1 <- data.frame(country = c("Italy", "Pakistan", "Brazil"), world_cups = c(4, 0, 5))
tab2 <- data.frame(exporter = c("DEU", "DEU", "ITA", "ITA"), HS_chapter = c(9, 85, 9, 85), volume = c(800, 5000, 1000, 2000))
tab3 <- data.frame(HS = c(9, 85), Description = c("Coffee, tea and mate", "Electrical machinery"))
tab4 <- data.frame(year = c(2010, 2011), Allemagne = runif(2), Brésil = runif(2), Pakistan = runif(2))

# These tables can easily be merged with one line of code:
auto_merge(tab1, tab2, tab3, tab4)


fbellelli/Countries documentation built on Feb. 25, 2025, 1:33 p.m.