[This article was first published on r.iresmi.net, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Today I saw this :
So, Portugal has only one terrestrial neighbour. Opencage lists the other countries with only one neighbour.
Can we get this by ourselves ?
library(tidyverse) library(rnaturalearth) library(spdep) # get data from Natural Earth countries <- ne_countries(scale = 10, returnclass = "sf") |> st_make_valid() |> group_by(sov_a3, sovereignt) |> summarise(.groups = "drop") # for joining codes and names later lookup <- countries |> st_drop_geometry() # get neighbours with {spdep} neighbours <- countries |> poly2nb() # we only keep countries with 1 neighbour # and add names neighbours |> 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…
And the « new » Canada / Denmark border is also not taken into account…
While we are at it, which country has more neighbors ?
neighbours |> 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…
# 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 # … with 199 more rows
To leave a comment for the author, please follow the link and comment on their blog: r.iresmi.net.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.