library(tidyverse)library(rnaturalearth)library(spdep)# get data from Natural Earthcountries <-ne_countries(scale =10, returnclass ="sf") |>st_make_valid() |>group_by(sov_a3, sovereignt) |>summarise(.groups ="drop")# for joining codes and names laterlookup <- countries |>st_drop_geometry()# get neighbours with {spdep}neighbours <- countries |>poly2nb()# we only keep countries with 1 neighbour# and add namesneighbours |>set_names(countries$sov_a3) |>keep(\(x) length(x) ==1) |>enframe("sov_a3", "nb") |>unnest(nb) |>filter(nb !=0) |>mutate(nb = countries$sov_a3[nb]) |>left_join(lookup, by ="sov_a3") |>left_join(lookup, by =c("nb"="sov_a3"), suffix =c("", "_nb")) |>relocate(sovereignt, .after =1)
# A tibble: 16 × 4
sov_a3 sovereignt nb sovereignt_nb
<chr> <chr> <chr> <chr>
1 BRN Brunei MYS Malaysia
2 CAN Canada US1 United States of America
3 DN1 Denmark DEU Germany
4 DOM Dominican Republic HTI Haiti
5 GMB Gambia SEN Senegal
6 HTI Haiti DOM Dominican Republic
7 IRL Ireland GB1 United Kingdom
8 KOR South Korea PRK North Korea
9 LSO Lesotho ZAF South Africa
10 MCO Monaco FR1 France
11 PNG Papua New Guinea IDN Indonesia
12 PRT Portugal ESP Spain
13 QAT Qatar SAU Saudi Arabia
14 SMR San Marino ITA Italy
15 TLS East Timor IDN Indonesia
16 VAT Vatican ITA Italy
I miss Bahrain / Saudi Arabia because the Natural Earth dataset is not detailed enough…
# A tibble: 209 × 3
sov_a3 n sovereignt
<chr> <int> <chr>
1 CH1 15 China
2 RUS 14 Russia
3 BRA 11 Brazil
4 FR1 11 France
5 COD 9 Democratic Republic of the Congo
6 DEU 9 Germany
7 AUT 8 Austria
8 SDN 8 Sudan
9 SRB 8 Republic of Serbia
10 TUR 8 Turkey
# ℹ 199 more rows
So, China…
However it includes the disputed part of « Kashmir », claimed by India and China, so it’s 14 neighbours, like Russia.
Source Code
---title: "One neighbour"description: "Contiguity analysis"author: "Michaël"date: "2023-03-26"date-modified: last-modifiedcategories: - R - spatialdraft: falsefreeze: trueeditor_options: chunk_output_type: console---Today I saw [this](https://en.osm.town/@opencage/110089029138109105) :![](image/image.png){fig-alt="A Mastodon post : Portugal 🇵🇹 borders only one country: Spain 🇪🇸"}So, Portugal has only one terrestrial neighbour. Opencage [lists the other countries with only one neighbour](https://blog.opencagedata.com/post/geotrivia-april2021-countries-that-border-only-one-country).Can we get this by ourselves ?```{r}#| label: configlibrary(tidyverse)library(rnaturalearth)library(spdep)# get data from Natural Earthcountries <-ne_countries(scale =10, returnclass ="sf") |>st_make_valid() |>group_by(sov_a3, sovereignt) |>summarise(.groups ="drop")# for joining codes and names laterlookup <- countries |>st_drop_geometry()# get neighbours with {spdep}neighbours <- countries |>poly2nb()# we only keep countries with 1 neighbour# and add namesneighbours |>set_names(countries$sov_a3) |>keep(\(x) length(x) ==1) |>enframe("sov_a3", "nb") |>unnest(nb) |>filter(nb !=0) |>mutate(nb = countries$sov_a3[nb]) |>left_join(lookup, by ="sov_a3") |>left_join(lookup, by =c("nb"="sov_a3"), suffix =c("", "_nb")) |>relocate(sovereignt, .after =1)```I miss Bahrain / Saudi Arabia because the [Natural Earth](https://www.naturalearthdata.com/) dataset is not detailed enough...![King Fahd Passport Island](image/image-1.png){fig-alt="Map of King Fahd Passport Island"}And the [« new » Canada / Denmark border](https://en.wikipedia.org/wiki/Whisky_War) is also not taken into account...While we are at it, which country has more neighbors?```{r}#| label: neighboursneighbours |>set_names(countries$sov_a3) |>unclass() |>enframe("sov_a3", "nb") |>unnest(nb) |>count(sov_a3, sort =TRUE) |>left_join(lookup, by ="sov_a3") ```So, China...However it includes the disputed part of « Kashmir », claimed by India and China, so it's 14 neighbours, like Russia.