Home > Software engineering >  Convert phone country codes to country names
Convert phone country codes to country names

Time:10-19

Is there a library or a way to convert phone country codes to country names. For example, convert 33 to France. I can't seem to find the phone country code as origin in library(countrycode).

CodePudding user response:

You can easily find a list of country dialling codes online and read them into a dataframe (say, using rvest). You can then use this to lookup the relevant country code.

For example, let's use the first hit I get when searching international telephone codes:

url <- paste0("https://warwick.ac.uk/services/academicoffice/",
              "ourservices/saro/recruitment/callingcampaign/callingcodes/")

If we parse the table on this page, we get a reasonable result:

library(rvest)

codes <- read_html(url) %>% 
  html_nodes(xpath = "//table") %>%
  html_table() %>%
  `[[`(1)

codes
#> # A tibble: 233 x 4
#>    Country        `Country Code` `International Prefix` `National Prefix`
#>   <chr>          <chr>          <chr>                  <chr>            
#>  1 Afghanistan     93            "00"                   "0"              
#>  2 Albania         355           "00"                   "0"              
#>  3 Algeria         213           "00"                   "7"              
#>  4 American Samoa  684           "00"                   ""               
#>  5 Andorra         376           "00"                   ""               
#>  6 Angola          244           "00"                   "0"              
#>  7 Anguilla        1-264         "011"                  "1"              
#>  8 Antarctica      672           ""                     ""               
#>  9 Antigua         1-268         "011"                  "1"              
#> 10 Argentina       54            "00"                   "0"              
#> # ... with 223 more rows

To get it into the format you want, we can remove the " " sign from the numbers in the second column:

codes$`Country Code` <- sub("\\ ", "", codes$`Country Code`)

And for ease of use, let's write a little function to look up our number codes:

lookup_code <- function(x) {
  codes$Country[codes$`Country Code` == x]
}

Which now allows:

lookup_code(33)
#> [1] "France"

lookup_code(44)
#> [1] "United Kingdom"

lookup_code(1)
#> [1] "Canada"         "USA Area Codes"

If you don't want to have to scrape a web page every time, here is a dataframe with "country" and "code" columns that you can use (taken from the same source):

df <- structure(list(country = c("Afghanistan", "Albania", "Algeria", 
"American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", 
"Antigua", "Argentina", "Armenia", "Aruba", "Ascension", "Australia", 
"Australian External Territories", "Austria", "Azerbaijan", "Bahamas", 
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", 
"Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia & Herzegovina", 
"Botswana", "Brazil", "British Virgin Islands", "Brunei Darussalam", 
"Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", 
"Canada", "Cape Verde Islands", "Cayman Islands", "Central African Republic", 
"Chad", "Chatham Islands (New Zealand)", "Chile", "China (PRC)", 
"Christmas Island", "Colombia", "Comoros", "Congo", "Congo(Democratic Republic of)", 
"Cook Islands", "Costa Rica", "Cote d'Ivoire (Ivory Coast)", 
"Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Diego Garcia", 
"Djibouti", "Dominica", "Dominican Republic", "East Timor", "Easter Island", 
"Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", 
"Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji Islands", 
"Finland", "France", "French Antilles", "French Guiana", "French Polynesia", 
"Gabonese Republic", "Gambia", "Georgia", "Germany", "Ghana", 
"Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", 
"Guam", "Guatemala", "Guinea Bissau", "Guinea", "Guyana", "Haiti", 
"Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", 
"Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", 
"Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea (North)", 
"Korea (South)", "Kuwait", "Kyrgyz Republic", "Laos", "Latvia", 
"Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", 
"Luxembourg", "Macao", "Macedonia", "Madagascar", "Malawi", "Malaysia", 
"Maldives", "Mali Republic", "Malta", "Marshall Islands", "Martinique", 
"Mauritania", "Mauritius", "Mayotte Island", "Mexico", "Micronesia", 
"Midway Island", "Moldova", "Monaco", "Mongolia", "Montserrat", 
"Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", 
"Netherlands", "Netherlands Antilles", "Nevis", "New Caledonia", 
"New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", 
"Northern Marine Islands", "Norway", "Oman", "Pakistan", "Palau", 
"Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", 
"Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion Island", 
"Romania", "Russia", "Rwanda", "St. Helena", "St. Kitts/Nevis", 
"St Lucia", "St. Pierre & Miquelon", "St. Vincent & Grenadines", 
"San Marino", "Sao Tomo and Principe", "Saudi Arabia", "Senegal", 
"Serbia & Montenegro", "Seychelles Republic", "Sierra Leone", 
"Singapore", "Slovak Republic", "Slovenia", "Solomon Islands", 
"Somalia", "South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname", 
"Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", 
"Tanzania", "Thailand", "Togolese Republic", "Tokelau", "Tonga Islands", 
"Trinidad & Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks & Caicos Islands", 
"Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", 
"USA Area Codes", "US Virgin Islands", "Uruguay", "Uzbekistan", 
"Vanuatu", "Vietnam", "Venezuela", "Yemen", "Zambia", "Zimbabwe"
), code = c("93", "355", "213", "684", "376", "244", "1-264", 
"672", "1-268", "54", "374", "297", "247", "61", "672", "43", 
"994", "1-242", "973", "880", "1-246", "375", "32", "501", "229", 
"1-441*", "975", "591", "387", "267", "55", "1-284*", "673*", 
"359", "226", "257", "855", "237", "1", "238", "1-345*", "236", 
"235", "64", "56", "86", "61\n        08 from Australia", "57", 
"269", "242", "243", "682", "506", "225", "385", "53", "357", 
"420", "45", "246", "253", "1-767*", "1-809*", "670", "56", "593", 
"20", "503", "240", "291", "372", "251", "500", "298", "679", 
"358", "33", "596", "594", "689", "241", "220", "995", "49", 
"233", "350", "30", "299", "1-473", "590", "1-671*", "502", "245", 
"224", "592", "509", "504", "852", "36", "354", "91", "62", "98", 
"964", "353", "972", "39", "1-876*", "81", "962", "7", "254", 
"686", "850", "82", "965", "996", "856", "371", "961", "266", 
"231", "218", "423", "370", "352", "853", "389", "261", "265", 
"60", "960", "223", "356", "692", "596", "222", "230", "269", 
"52", "691", "1-808*", "373", "377", "976", "1-664*", "212", 
"258", "95", "264", "674", "977", "31", "599", "1-869*", "687", 
"64", "505", "227", "234", "683", "672", "1-670*", "47", "968", 
"92", "680", "507", "675", "595", "51", "63", "48", "351", "1-787 or  1", 
"974", "262", "40", "7", "250", "290", "1-869*", "1-758*", "508", 
"1-784*", "378", "239", "966", "221", "381", "248", "232", "65", 
"421", "386", "677", "252", "27", "34", "94", "249", "597", "268", 
"46", "41", "963", "886", "992", "255", "66", "228", "690", "676", 
"1-868", "216", "90", "993", "1~649", "688", "256", "380", "971", 
"44", "1", "1-340*", "598", "998", "678", "84", "58", "998", 
"260", "263")), row.names = c(NA, -233L), class = "data.frame")
  • Related