Skip to content

Instantly share code, notes, and snippets.

@robertjwilson
Last active April 26, 2019 19:59
Show Gist options
  • Select an option

  • Save robertjwilson/e2f817a7320d11cb3eaf819aa5e7e555 to your computer and use it in GitHub Desktop.

Select an option

Save robertjwilson/e2f817a7320d11cb3eaf819aa5e7e555 to your computer and use it in GitHub Desktop.
# simple function to left join countries based on country names. Works with varients; UK/United Kingdom etc.
country_join <- function(x,y, by = "Country"){
col_2use <- sym(by)
dplyr::mutate(x, country_code54321 = countrycode::countrycode(!!col_2use, "country.name", "iso3c")) %>%
left_join(
dplyr::mutate(y, country_code54321 = countrycode::countrycode(!!col_2use, "country.name", "iso3c")), by = "country_code54321") %>%
dplyr::select(-country_code54321)
}
# example
library(tidyverse)
x <- data_frame(Country = "UK", output = 1)
y <- data_frame(Country = "United Kingdom", output2 = 2)
left_join(x,y)
country_join(x,y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment