rcountry-codes

Determining Neighboring Countries


I have a dataset that contains country abbreviations based on ISO-3 codes, as well as some of their values.

library(dplyr)

# Create a dataframe with ISO-3 codes and random values
iso3 <- c("USA", "CAN", "GBR", "FRA", "GER")
random_values <- runif(5, min = 1, max = 100)  # Generating random values between 1 and 100

# Combine ISO-3 codes and random values into a dataframe
country_data <- data.frame(ISO_3 = iso3, Random_Values = random_values)

But now, I have a new challenge before me. This challenge consists of the fact that I want to first select a specific country and then find its neighboring countries.

Can someone help me with how to find the neighboring countries of an already selected country?


Solution

  • With the data you have provided, there isn't any way to do this. However, GeoDataSource provides this data for free from their "Download" page (scroll down to the very bottom to the "Country Borders").

    You can easily read this data into R using read.csv.

    data <- read.csv("GEODATASOURCE-COUNTRY-BORDERS.CSV")
    head(data)
    

      country_code         country_name country_border_code        country_border_name
    1           AD              Andorra                  FR                     France
    2           AD              Andorra                  ES                      Spain
    3           AE United Arab Emirates                  OM                       Oman
    4           AE United Arab Emirates                  SA               Saudi Arabia
    5           AF          Afghanistan                  CN                      China
    6           AF          Afghanistan                  IR Iran (Islamic Republic of)
    

    The data contains the ISO 2-digit codes only. If you need the ISO 3-digit codes, you can link this data to the ISO_3166_1 dataset provided by the ISOcodes package.

    library(ISOcodes)
    data(ISO_3166_1)
    head(ISO_3166_1)
    

      Alpha_2 Alpha_3 Numeric          Name                   Official_name Common_name
    1      AW     ABW     533         Aruba                            <NA>        <NA>
    2      AF     AFG     004   Afghanistan Islamic Republic of Afghanistan        <NA>
    3      AO     AGO     024        Angola              Republic of Angola        <NA>
    4      AI     AIA     660      Anguilla                            <NA>        <NA>
    5      AX     ALA     248 Åland Islands                            <NA>        <NA>
    6      AL     ALB     008       Albania             Republic of Albania        <NA>
    

    library(dplyr)
    
    data <- inner_join(data, select(ISO_3166_1, Alpha_2, Alpha_3),
                             by=c("country_code"="Alpha_2"))
    head(data)
    

      country_code         country_name country_border_code        country_border_name Alpha_3
    1           AD              Andorra                  FR                     France     AND
    2           AD              Andorra                  ES                      Spain     AND
    3           AE United Arab Emirates                  OM                       Oman     ARE
    4           AE United Arab Emirates                  SA               Saudi Arabia     ARE
    5           AF          Afghanistan                  CN                      China     AFG
    6           AF          Afghanistan                  IR Iran (Islamic Republic of)     AFG
    

    To see which country has the most border countries:

    count(data, Alpha_3) |> slice_max(n)
    

      Alpha_3  n
    1     CHN 16
    

    Select any particular country and show which countries are its neighbors.

    filter(data, Alpha_3=="CHN")
    

       country_code country_name country_border_code                     country_border_name Alpha_3
    1            CN        China                  AF                             Afghanistan     CHN
    2            CN        China                  BT                                  Bhutan     CHN
    3            CN        China                  HK                               Hong Kong     CHN
    4            CN        China                  IN                                   India     CHN
    5            CN        China                  KZ                              Kazakhstan     CHN
    6            CN        China                  KP Korea (Democratic People's Republic of)     CHN
    7            CN        China                  KG                              Kyrgyzstan     CHN
    8            CN        China                  LA        Lao People's Democratic Republic     CHN
    9            CN        China                  MO                                   Macao     CHN
    10           CN        China                  MN                                Mongolia     CHN
    11           CN        China                  MM                                 Myanmar     CHN
    12           CN        China                  NP                                   Nepal     CHN
    13           CN        China                  PK                                Pakistan     CHN
    14           CN        China                  RU                      Russian Federation     CHN
    15           CN        China                  TJ                              Tajikistan     CHN
    16           CN        China                  VN                                Viet Nam     CHN
    

    Just for fun, you can plot this using a variety of R packages:

    library("sf")
    library("ggplot2")
    library("rnaturalearth")
    library("rnaturalearthdata")
    
    world <- ne_countries(scale = "medium", returnclass = "sf")
    
    China <- filter(data, Alpha_3=="CHN") |>
      mutate(country_border_name=case_when(  # Fix some inconsistencies
        country_border_name=="Russian Federation"~"Russia",
        country_border_name=="Korea (Democratic People's Republic of)"~"North Korea",
        country_border_name=="Lao People's Democratic Republic"~"Laos",
        country_border_name=="Viet Nam"~"Vietnam",
        TRUE~country_border_name))
    
    China_map <- world %>% 
      mutate(value = factor(case_when(admin %in% "China"~"China",
                                      admin %in% China$country_border_name~"Neighbour",
                                      TRUE~"Other")))
    ggplot(data = China_map) +
      geom_sf(aes(fill=value)) +
      labs(fill="")
    

    enter image description here