Skip to contents

This function retrieves a list of countries based on a specified region, subregion, or continent. The function is case-insensitive and orders the countries alphabetically by their common names. If the input does not match any region, subregion, or continent, the function provides a list of all available regions, subregions, and continents.

Usage

get_countries_by_region(country_region_value)

Arguments

country_region_value

A character string representing the region, subregion, or continent. The input is case-insensitive.

Value

A data frame containing the list of countries within the specified region, subregion, or continent, ordered alphabetically by country name. If no match is found, a warning message is displayed, and a list of all available regions, subregions, and continents is provided.

Note

The function utilizes the pre-loaded restcountries_data dataset. Ensure that this dataset is loaded before invoking the function. The selected columns include country codes, names, capital, region, subregion, start of the week, car side, currencies, population, latitude, and longitude.

Examples

# \donttest{
# Example usage: Get a list of countries in Africa
africa_countries <- get_countries_by_region("Africa")
print(africa_countries)
#> # A tibble: 61 × 19
#>    cca3  cca2  common_name     official_name capital region subregion continents
#>    <chr> <chr> <chr>           <chr>         <chr>   <chr>  <chr>     <chr>     
#>  1 DZA   DZ    Algeria         People's Dem… Algiers Africa Northern… Africa    
#>  2 AGO   AO    Angola          Republic of … Luanda  Africa Middle A… Africa    
#>  3 BEN   BJ    Benin           Republic of … Porto-… Africa Western … Africa    
#>  4 BWA   BW    Botswana        Republic of … Gaboro… Africa Southern… Africa    
#>  5 IOT   IO    British Indian… British Indi… Diego … Africa Eastern … Asia      
#>  6 BFA   BF    Burkina Faso    Burkina Faso  Ouagad… Africa Western … Africa    
#>  7 BDI   BI    Burundi         Republic of … Gitega  Africa Eastern … Africa    
#>  8 CMR   CM    Cameroon        Republic of … Yaoundé Africa Middle A… Africa    
#>  9 CPV   CV    Cape Verde      Republic of … Praia   Africa Western … Africa    
#> 10 CAF   CF    Central Africa… Central Afri… Bangui  Africa Middle A… Africa    
#> # ℹ 51 more rows
#> # ℹ 11 more variables: un_member <lgl>, landlocked <lgl>, timezones <chr>,
#> #   start_of_week <chr>, car_side <chr>, currencies <chr>, population <int>,
#> #   area <dbl>, root <chr>, lat <dbl>, lon <dbl>

# Example usage: Get a list of countries in Western Europe (a subregion)
western_europe_countries <- get_countries_by_region("Western Europe")
print(western_europe_countries)
#> # A tibble: 8 × 19
#>   cca3  cca2  common_name   official_name    capital region subregion continents
#>   <chr> <chr> <chr>         <chr>            <chr>   <chr>  <chr>     <chr>     
#> 1 BEL   BE    Belgium       Kingdom of Belg… Brusse… Europe Western … Europe    
#> 2 FRA   FR    France        French Republic  Paris   Europe Western … Europe    
#> 3 DEU   DE    Germany       Federal Republi… Berlin  Europe Western … Europe    
#> 4 LIE   LI    Liechtenstein Principality of… Vaduz   Europe Western … Europe    
#> 5 LUX   LU    Luxembourg    Grand Duchy of … Luxemb… Europe Western … Europe    
#> 6 MCO   MC    Monaco        Principality of… Monaco  Europe Western … Europe    
#> 7 NLD   NL    Netherlands   Kingdom of the … Amster… Europe Western … Europe    
#> 8 CHE   CH    Switzerland   Swiss Confedera… Bern    Europe Western … Europe    
#> # ℹ 11 more variables: un_member <lgl>, landlocked <lgl>, timezones <chr>,
#> #   start_of_week <chr>, car_side <chr>, currencies <chr>, population <int>,
#> #   area <dbl>, root <chr>, lat <dbl>, lon <dbl>

# Example usage: Get a list of countries in the continent of Asia
asia_countries <- get_countries_by_region("Asia")
print(asia_countries)
#> # A tibble: 56 × 19
#>    cca3  cca2  common_name     official_name capital region subregion continents
#>    <chr> <chr> <chr>           <chr>         <chr>   <chr>  <chr>     <chr>     
#>  1 AFG   AF    Afghanistan     Islamic Repu… Kabul   Asia   Southern… Asia      
#>  2 ARM   AM    Armenia         Republic of … Yerevan Asia   Western … Asia      
#>  3 AZE   AZ    Azerbaijan      Republic of … Baku    Asia   Western … Europe    
#>  4 AZE   AZ    Azerbaijan      Republic of … Baku    Asia   Western … Asia      
#>  5 BHR   BH    Bahrain         Kingdom of B… Manama  Asia   Western … Asia      
#>  6 BGD   BD    Bangladesh      People's Rep… Dhaka   Asia   Southern… Asia      
#>  7 BTN   BT    Bhutan          Kingdom of B… Thimphu Asia   Southern… Asia      
#>  8 IOT   IO    British Indian… British Indi… Diego … Africa Eastern … Asia      
#>  9 BRN   BN    Brunei          Nation of Br… Bandar… Asia   South-Ea… Asia      
#> 10 KHM   KH    Cambodia        Kingdom of C… Phnom … Asia   South-Ea… Asia      
#> # ℹ 46 more rows
#> # ℹ 11 more variables: un_member <lgl>, landlocked <lgl>, timezones <chr>,
#> #   start_of_week <chr>, car_side <chr>, currencies <chr>, population <int>,
#> #   area <dbl>, root <chr>, lat <dbl>, lon <dbl>
# }