In my dataset I have only the country codes given. I managed to get the full country names, however for some reason I cannot create a new column with the full country names & consequently cannot create a new column with the corresponding continent.
That is what I have done so far:
mycodes <- c("PRT", "FRA", "JPN", "IRL", "ESP", "BEL", "AUT", "DEU", "ITA", "CHN", "RUS", "POL", "USA", "CRI", "CHE", "ROU", "GBR",
"BRA", "FIN", "NLD", "CAN", "ZAF", "AUS", "AGO", "BGR", "SWE", "CYP", "ARG", "ARM", "CHL", "MOZ", "KOR", "TUN", "DNK", "GRC",
"NOR", "ISR", "MYS", "EGy", "JOR", "LUX", "TUR", "IRN", "LBY", "PAN", "COL", "VEN", "DZA", "GNB", "MAR", "CZE", "SVN", "IND",
"HUN", "NZL", "PER", "LTU", "TWN", "SRB", "EST", "KAZ", "KWT", "IDN", "UKR", "MEX", "SVK", "SAU", "ARE", "BGD", "THA", "TZA",
"LVA", "PHL", "BIH", "BHR", "NAM", "BOL", "HRV", "SGP", "CMR", "MLT", "URY", "PAK", "JAM", "ECU", "SYC", "QAT", "PRY", "BRB",
"OMN", "TMP", "ABW", "LBN", "SLV", "DMA", "CUB", "VNM", "GEO", "IRQ", "PYF", "UGA", "LIE", "SEN", "BLR", "ISL", "DOM",
"GUY", "LCA", "CPV", "ATA", "GAB", "NGA", "RWA", "CIV", "ALB", "MKD", "MNE", "GTM", "GHA", "MDV", "MCO", "MUS", "TGO", "LKA",
"AZE", "SUR", "KEN", "MRT", "HKG", "SYR", "CAF", "NCL", "UZB", "KIR", "SDN", "PRI", "ATF", "KNA", "TJK", "SLE", "LAO", "COM",
"ETH", "FRO", "AND", "BEN", "ZWE", "ASM", "MLI", "BWA", "AIA", "COD", "SPM", "JEY", "MDG", "NIC", "SWZ", "CYM", "SOM", "ATG",
"KGZ", "FLK", "GIB", "SMR", "TKM", "HTI", "UMI", "MMR", "WSM", "VIR", "ERI", "WLF", "GUF", "MWI", "PCN", "TCD")
countrycode(mycodes, "iso3c", "country.name")
x <- c(countrycode)
mutate(NEWHotelCustomers, Nationality_Customers = countrycode(mycodes, "iso3c", "country.name"))
View(NEWHotelCustomers)
That is the error message I received:
Error in `mutate()`:
! Problem while computing `Nationality_Customers = countrycode(mycodes, "iso3c", "country.name")`.
x `Nationality_Customers` must be size 58248 or 1, not 176.
Run `rlang::last_error()` to see where the error occurred.
Warning message:
Problem while computing `Nationality_Customers = countrycode(mycodes, "iso3c", "country.name")`.
i Some values were not matched unambiguously: TMP
Thanks in advance!
CodePudding user response:
Are you looking for
library(countrycode)
df <- data.frame(country_code = mycodes,
country_name = countrycode(sourcevar = mycodes, origin = "iso3c",
destination = "country.name"),
continent = countrycode(sourcevar = mycodes, origin = "iso3c",
destination = "continent"))
head(df)
?
Note, countrycode()
does not manage to identify all strings "iso3c" (country codes) unambiguously. See the warnings to identify which
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: TMP
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: ATA, ATF, TMP, UMI
Btw, I see no need to use {dplyr}
here.
Data
mycodes <- c("PRT", "FRA", "JPN", "IRL", "ESP", "BEL", "AUT", "DEU", "ITA", "CHN", "RUS", "POL", "USA", "CRI", "CHE", "ROU", "GBR",
"BRA", "FIN", "NLD", "CAN", "ZAF", "AUS", "AGO", "BGR", "SWE", "CYP", "ARG", "ARM", "CHL", "MOZ", "KOR", "TUN", "DNK", "GRC",
"NOR", "ISR", "MYS", "EGy", "JOR", "LUX", "TUR", "IRN", "LBY", "PAN", "COL", "VEN", "DZA", "GNB", "MAR", "CZE", "SVN", "IND",
"HUN", "NZL", "PER", "LTU", "TWN", "SRB", "EST", "KAZ", "KWT", "IDN", "UKR", "MEX", "SVK", "SAU", "ARE", "BGD", "THA", "TZA",
"LVA", "PHL", "BIH", "BHR", "NAM", "BOL", "HRV", "SGP", "CMR", "MLT", "URY", "PAK", "JAM", "ECU", "SYC", "QAT", "PRY", "BRB",
"OMN", "TMP", "ABW", "LBN", "SLV", "DMA", "CUB", "VNM", "GEO", "IRQ", "PYF", "UGA", "LIE", "SEN", "BLR", "ISL", "DOM",
"GUY", "LCA", "CPV", "ATA", "GAB", "NGA", "RWA", "CIV", "ALB", "MKD", "MNE", "GTM", "GHA", "MDV", "MCO", "MUS", "TGO", "LKA",
"AZE", "SUR", "KEN", "MRT", "HKG", "SYR", "CAF", "NCL", "UZB", "KIR", "SDN", "PRI", "ATF", "KNA", "TJK", "SLE", "LAO", "COM",
"ETH", "FRO", "AND", "BEN", "ZWE", "ASM", "MLI", "BWA", "AIA", "COD", "SPM", "JEY", "MDG", "NIC", "SWZ", "CYM", "SOM", "ATG",
"KGZ", "FLK", "GIB", "SMR", "TKM", "HTI", "UMI", "MMR", "WSM", "VIR", "ERI", "WLF", "GUF", "MWI", "PCN", "TCD")
CodePudding user response:
First, create a tibble/dataframe from the mycodes-vector such that you can feed it in dplyr pipe. Use the countrycode::countrycode()
-function in a dplyr::mutate()
-call to attach the new variable scontaining the countrynames and the respective continent:
library(countrycode)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
mycodes <- c("PRT", "FRA", "JPN", "IRL", "ESP", "BEL", "AUT", "DEU", "ITA", "CHN", "RUS", "POL", "USA", "CRI", "CHE",
"ROU", "GBR", "BRA", "FIN", "NLD", "CAN", "ZAF", "AUS", "AGO", "BGR", "SWE", "CYP", "ARG", "ARM", "CHL",
"MOZ", "KOR", "TUN", "DNK", "GRC", "NOR", "ISR", "MYS", "EGy", "JOR", "LUX", "TUR", "IRN", "LBY", "PAN",
"COL", "VEN", "DZA", "GNB", "MAR", "CZE", "SVN", "IND", "HUN", "NZL", "PER", "LTU", "TWN", "SRB", "EST",
"KAZ", "KWT", "IDN", "UKR", "MEX", "SVK", "SAU", "ARE", "BGD", "THA", "TZA", "LVA", "PHL", "BIH", "BHR",
"NAM", "BOL", "HRV", "SGP", "CMR", "MLT", "URY", "PAK", "JAM", "ECU", "SYC", "QAT", "PRY", "BRB", "OMN",
"TMP", "ABW", "LBN", "SLV", "DMA", "CUB", "VNM", "GEO", "IRQ", "PYF", "UGA", "LIE", "SEN", "BLR", "ISL",
"DOM", "GUY", "LCA", "CPV", "ATA", "GAB", "NGA", "RWA", "CIV", "ALB", "MKD", "MNE", "GTM", "GHA", "MDV",
"MCO", "MUS", "TGO", "LKA", "AZE", "SUR", "KEN", "MRT", "HKG", "SYR", "CAF", "NCL", "UZB", "KIR", "SDN",
"PRI", "ATF", "KNA", "TJK", "SLE", "LAO", "COM", "ETH", "FRO", "AND", "BEN", "ZWE", "ASM", "MLI", "BWA",
"AIA", "COD", "SPM", "JEY", "MDG", "NIC", "SWZ", "CYM", "SOM", "ATG", "KGZ", "FLK", "GIB", "SMR", "TKM",
"HTI", "UMI", "MMR", "WSM", "VIR", "ERI", "WLF", "GUF", "MWI", "PCN", "TCD")
df <- tibble(codes = mycodes)
df <- df %>%
mutate(countryname = countrycode(mycodes, "iso3c", "country.name"),
continent = countrycode(mycodes, "iso3c", "continent") )
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: TMP
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: ATA, ATF, TMP, UMI
head(df)
#> # A tibble: 6 x 3
#> codes countryname continent
#> <chr> <chr> <chr>
#> 1 PRT Portugal Europe
#> 2 FRA France Europe
#> 3 JPN Japan Asia
#> 4 IRL Ireland Europe
#> 5 ESP Spain Europe
#> 6 BEL Belgium Europe
Created on 2022-05-26 by the reprex package (v0.3.0)