I'm having an annoying problem that I'm trying to find an automated solution for. The shorthand version is that I have a shapefile and a table of created data for regions within countries. The created data table does NOT have any sort of standardized GIDs/admin codes to match to shapefiles, and the region names are not exact matches either. Let's take a closer look; here's my dummy data frame + shapefile.
library(rgdal) #load in shapefile arm <- readOGR("D:/Country-Shapefiles/ARM_adm_shp", layer = "ARM_adm1") #create dummy data frame id <- c(100:110) name <- c("Aragatsotn", "Ararat", "Armavir", "Gaghark'unik'", "Kotayk", "Lorri", "Shirak", "Syunik'", "Tavush", "Vayots' Dzor", "Yerevan City") value <- runif(11, 0.0, 1.0) df <- data.frame(id, name, value)
So what I have is a table with seemingly random IDs, region names, and a value to be plotted with a choropleth map. Looks like this:
> df id name value 1 100 Aragatsotn 0.6923852 2 101 Ararat 0.5762024 3 102 Armavir 0.4688358 4 103 Gaghark'unik' 0.4702253 5 104 Kotayk 0.9347992 6 105 Lorri 0.1937813 7 106 Shirak 0.5162604 8 107 Syunik' 0.4332389 9 108 Tavush 0.9889513 10 109 Vayots' Dzor 0.2182024 11 110 Yerevan City 0.5791886
Looking at the shapefile attributes of interest, we've got this:
> arm@data[c("ID_1", "NAME_1")] ID_1 NAME_1 0 1 Aragatsotn 1 2 Ararat 2 3 Armavir 3 4 Erevan 4 5 Gegharkunik 5 6 Kotayk 6 7 Lori 7 8 Shirak 8 9 Syunik 9 10 Tavush 10 11 Vayots Dzor
df would include some sort of matching admin IDs to join to the shapefile. Whoever created the data that I'm using did not follow these conventions, unfortunately. Alternatively, it'd be great to match the region names themselves...but as you can see, there are slight variations in each name.
Matching by hand is always a backup solution, but who wants to take the time to do that? ;) But really, even barring laziness, the project that I'm working on will be mapping dozens and dozens of different countries, so I'm looking for an automated solution that can do everything without having to do anything by hand. Is this possible? Can I somehow match these -almost- region names to the shapefiles?
Sidenote: I'm looking into
grepl for partial string matches per this post, but I'm not sure if this is a potential solution because I'll need to draw from the column names rather than inputting each region name by hand.
EDIT: When I match the IDs by hand, what I've done is create a new column in my data frame and adding the exact matching terms from the shapefile. Unfortunately, because of the peculiarities of the data, the order of the names don't match up either, so this still requires some manual input. I'm hoping for some sort of completely automated solution (if it's even possible).