Last active
April 26, 2019 19:59
-
-
Save robertjwilson/e2f817a7320d11cb3eaf819aa5e7e555 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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