2

I have two dataframes with a common column that contains country names of the world. But both these dataframes do not follow the same format of country names. For example, one data frame states North Korea,while the other says Dem People's Rep of Korea. Another example is Macedonia in one dataframe, while the other says The former Yugoslav Republic of Macedonia.

I am looking for a way to merge both dataframes on this country column, since it is the only common column. And I will have to do this multiple times over many different dataframes.

I tried

t<-as.data.frame(sapply(data, function(x) gsub("Yugoslav", "Macedonia",x))) 

but this searches for the word Yugoslav in the dataframe and only replaces Yugoslav and not the entire The former Yugoslav Republic of Macedonia.

Help appreciated.

3
  • If down voted, please provide reasons for doing so.
    – tg110
    Commented Mar 28, 2017 at 17:31
  • Depending on the number of countries, I would probably build a lookup table (a named vector) and use that to transform one of the columns or construct a new column. See this post in SO Documentation. Without example data, it is difficult to give more advice.
    – lmo
    Commented Mar 28, 2017 at 17:45
  • @lmo- there are about 25-30 countries. I will look into the link you provided. As for data- I have explained pretty much all of it. Country names in one column in df1 needs to match country names in one column in df2
    – tg110
    Commented Mar 28, 2017 at 19:38

3 Answers 3

1

The countrycode package is your friend. Description:

Standardize country names, convert them into one of eleven coding schemes, convert between coding schemes, and assign region descriptors.

for example...

countrycode(c("Macedonia", "The former Yugoslav Republic of Macedonia"), "country.name", "country.name")
[1] "Macedonia, the former Yugoslav Republic of"
[2] "Macedonia, the former Yugoslav Republic of"

countrycode(c("North Korea", "Dem People's Rep of Korea"), "country.name", "iso3c")
[1] "PRK" "PRK"
4
  • @dash2- Not really. The datasets I am using do not follow any of the schemes found in the package. I looked into this as well.
    – tg110
    Commented Mar 28, 2017 at 18:01
  • @tg110 you stated that your datasets contain country names... the countrycode package definitely works with country names... e.g. countrycode(data$country, "country.name", "iso3c")
    – CJ Yetman
    Commented Mar 29, 2017 at 6:51
  • @tg110 or some better examples specific to your question... countrycode(c("Macedonia", "The former Yugoslav Republic of Macedonia"), "country.name", "country.name") will return... [1] "Macedonia, the former Yugoslav Republic of" [2] "Macedonia, the former Yugoslav Republic of" countrycode(c("North Korea", "Dem People's Rep of Korea"), "country.name", "iso3c") will return... [1] "PRK" "PRK"
    – CJ Yetman
    Commented Mar 29, 2017 at 6:58
  • @tg110 seems like some of your examples will work. If others don't, you could create a custom country code dictionary: see github.com/vincentarelbundock/countrycode
    – user3603486
    Commented Mar 29, 2017 at 11:41
0

You can use a combination of

to match country names. Example code below:

library(RgoogleMaps)
library(sp)
library(rworldmap)

# <-- Copy code for coords2country here. -->

# Geocode location names.
geo_1 <- getGeoCode("Dem People's Rep of Korea")
geo_2 <- getGeoCode("North Korea")

# Transform geocodes to appropriate data tables.
geo_1 <- data.table(t(rev(geo_1)))
geo_2 <- data.table(t(rev(geo_2)))

# Reverse geocode coordinates to country names.
country_1 <- as.character(coords2country(geo_1))
country_2 <- as.character(coords2country(geo_2))

When country_1 and country_2 are the same, then the locations you started with are very likely to be the same. Nothing guaranteed of course.

0

@dash2 has already provided what I believe is the best answer assuming you're working with country names, however, if you're looking for something more general and/or something that achieves exactly what the title of this question calls for or more closely matches your example, here's an idea...

The first/pattern argument of gsub is a regex, so you can match any character/s before and or after your match string by adding ".*" before and after it like this...

gsub(".*Yugoslav.*", "Macedonia", "The former Yugoslav Republic of Macedonia")

So your exact example from above, fixed to work how it sounds like you wanted would look like...

t<-as.data.frame(sapply(data, function(x) gsub(".*Yugoslav.*", "Macedonia",x)))

Note: gsub is vectorised, i.e. it accepts a character vector for the x argument, so there's no need to run it through sapply. The following two commands are roughly equivalent (though sapply adds names to the result)...

sapply(data, function(x) gsub("Yugoslav", "Macedonia",x))
gsub("Yugoslav", "Macedonia", data)

So a better version of your exact example would be...

t <- as.data.frame(gsub(".*Yugoslav.*", "Macedonia", data))

Assuming you have a data frame df with a column/vector named country which contains strings, the following code would change (entirely) any values that contain "Yugoslav" to "Macedonia" (including strings such as "The former Yugoslav Republic of Macedonia") in the existing data frame...

df$country <- gsub(".*Yugoslav.*", "Macedonia", df$country)

You would need a separate command for each set of strings you want to swap, so I don't think this is the best way to achieve it if you have many changes to make, but maybe you were only interested in making a few changes, or maybe you want to do each one manually for some reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.