Search code examples
rcountry-codes

Is there a function in R that lets me create a new column with (1) the full country names and (2) the respective continent?


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!


Solution

  • 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")