Categories countries into continents

Is there anyone who knows how to categories the countries in my data into continents?

My data (it's called Ximputed):

I have tried to use following code but it doesn't work at all

library(rnaturalearth)
library(sf)
Region <- ne_countries(country = Ximputed$country, returnclass = "sf")
Region <- rename.vars(Region, 'sovereignt', 'country')
x2 <- Ximputed %>% 
  left_join(Region, "country")

Hi, I usually use the country_code package. Here is a sample of my code, usually it's easier to match the continents/regions with the specific country codes (as some names have different variations). If you don't have the country codes, you will have to adapt the code to match with the country names (usually some can't be matched and have to be matched "by hand").

df1 = df1 %>%
  mutate(country_region = countrycode(country_code, origin = "iso3n", destination = "region"))

df1 = df1 %>%
  mutate(continent = countrycode(country_code, origin = "iso3n", destination = "continent"))

Hope this helps

Thanks! but sadly i don't have the country codes - do you know how to convert the code so i can use the country names from my data?

You can use the countrycode package to either match the country names with the corresponding (world bank) region or continent or you use the package to match the country names with their country codes and then with their continent. If you don't need the codes, I would try to match them directly

You could use this list and join it to your data frame:

library(tidyverse)

Ximputed <- tibble(country = c("Austria", "Algeria", "Brazil"),
                   barley = c(13, 22, 9),
                   maize  = c(15, 25, 10),
                   rice = c(10, 17, 8))

continent_list <- structure(list(country = c("Afghanistan", "Åland Islands", "Albania", 
                                "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", 
                                "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", 
                                "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", 
                                "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", 
                                "Bermuda", "Bhutan", "Bolivia (Plurinational State of)", "Bonaire, Sint Eustatius and Saba", 
                                "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", 
                                "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", 
                                "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", 
                                "Cabo Verde", "Cayman Islands", "Central African Republic", "Chad", 
                                "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", 
                                "Colombia", "Comoros", "Congo", "Congo (Democratic Republic of the)", 
                                "Cook Islands", "Costa Rica", "Côte d'Ivoire", "Croatia", "Cuba", 
                                "Curaçao", "Cyprus", "Czech Republic", "Denmark", "Djibouti", 
                                "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", 
                                "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", 
                                "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", 
                                "French Polynesia", "French Southern Territories", "Gabon", "Gambia", 
                                "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", 
                                "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", 
                                "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", 
                                "Holy See", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", 
                                "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", 
                                "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", 
                                "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea (Democratic People's Republic of)", 
                                "Korea (Republic of)", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", 
                                "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", 
                                "Lithuania", "Luxembourg", "Macao", "Macedonia (the former Yugoslav Republic of)", 
                                "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", 
                                "Marshall Islands", "Martinique", "Mauritania", "Mauritius", 
                                "Mayotte", "Mexico", "Micronesia (Federated States of)", "Moldova (Republic of)", 
                                "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", 
                                "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", 
                                "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", 
                                "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", 
                                "Oman", "Pakistan", "Palau", "Palestine, State of", "Panama", 
                                "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", 
                                "Poland", "Portugal", "Puerto Rico", "Qatar", "Réunion", "Romania", 
                                "Russian Federation", "Rwanda", "Saint Barthélemy", "Saint Helena, Ascension and Tristan da Cunha", 
                                "Saint Kitts and Nevis", "Saint Lucia", "Saint Martin (French part)", 
                                "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", 
                                "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", 
                                "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", 
                                "Sint Maarten (Dutch part)", "Slovakia", "Slovenia", "Solomon Islands", 
                                "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", 
                                "South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", 
                                "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", 
                                "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", 
                                "Thailand", "Timor-Leste", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", 
                                "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", 
                                "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom of Great Britain and Northern Ireland", 
                                "United States of America", "United States Minor Outlying Islands", 
                                "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela (Bolivarian Republic of)", 
                                "Viet Nam", "Virgin Islands (British)", "Virgin Islands (U.S.)", 
                                "Wallis and Futuna", "Western Sahara", "Yemen", "Zambia", "Zimbabwe"
), continent = c("Asia", "Europe", "Europe", "Africa", "Oceania", 
                 "Europe", "Africa", "Americas", NA, "Americas", "Americas", "Asia", 
                 "Americas", "Oceania", "Europe", "Asia", "Americas", "Asia", 
                 "Asia", "Americas", "Europe", "Europe", "Americas", "Africa", 
                 "Americas", "Asia", "Americas", "Americas", "Europe", "Africa", 
                 NA, "Americas", NA, "Asia", "Europe", "Africa", "Africa", "Asia", 
                 "Africa", "Americas", "Africa", "Americas", "Africa", "Africa", 
                 "Americas", "Asia", NA, NA, "Americas", "Africa", "Africa", "Africa", 
                 "Oceania", "Americas", "Africa", "Europe", "Americas", "Americas", 
                 "Asia", "Europe", "Europe", "Africa", "Americas", "Americas", 
                 "Americas", "Africa", "Americas", "Africa", "Africa", "Europe", 
                 "Africa", "Americas", "Europe", "Oceania", "Europe", "Europe", 
                 "Americas", "Oceania", NA, "Africa", "Africa", "Asia", "Europe", 
                 "Africa", "Europe", "Europe", "Americas", "Americas", "Americas", 
                 "Oceania", "Americas", "Europe", "Africa", "Africa", "Americas", 
                 "Americas", NA, "Europe", "Americas", "Asia", "Europe", "Europe", 
                 "Asia", "Asia", "Asia", "Asia", "Europe", "Europe", "Asia", "Europe", 
                 "Americas", "Asia", "Europe", "Asia", "Asia", "Africa", "Oceania", 
                 "Asia", "Asia", "Asia", "Asia", "Asia", "Europe", "Asia", "Africa", 
                 "Africa", "Africa", "Europe", "Europe", "Europe", "Asia", "Europe", 
                 "Africa", "Africa", "Asia", "Asia", "Africa", "Europe", "Oceania", 
                 "Americas", "Africa", "Africa", "Africa", "Americas", "Oceania", 
                 "Europe", "Europe", "Asia", "Europe", "Americas", "Africa", "Africa", 
                 "Asia", "Africa", "Oceania", "Asia", "Europe", "Oceania", "Oceania", 
                 "Americas", "Africa", "Africa", "Oceania", "Oceania", "Oceania", 
                 "Europe", "Asia", "Asia", "Oceania", "Asia", "Americas", "Oceania", 
                 "Americas", "Americas", "Asia", "Oceania", "Europe", "Europe", 
                 "Americas", "Asia", "Africa", "Europe", "Europe", "Africa", "Americas", 
                 "Africa", "Americas", "Americas", "Americas", "Americas", "Americas", 
                 "Oceania", "Europe", "Africa", "Asia", "Africa", "Europe", "Africa", 
                 "Africa", "Asia", "Americas", "Europe", "Europe", "Oceania", 
                 "Africa", "Africa", NA, "Africa", "Europe", "Asia", "Africa", 
                 "Americas", "Europe", "Africa", "Europe", "Europe", "Asia", "Asia", 
                 "Asia", "Africa", "Asia", "Asia", "Africa", "Oceania", "Oceania", 
                 "Americas", "Africa", "Asia", "Asia", "Americas", "Oceania", 
                 "Africa", "Europe", "Asia", "Europe", "Americas", NA, "Americas", 
                 "Asia", "Oceania", "Americas", "Asia", "Americas", "Americas", 
                 "Oceania", "Africa", "Asia", "Africa", "Africa")), row.names = c(NA, 
                                                                                  -249L), class = c("tbl_df", "tbl", "data.frame"))

left_join(Ximputed, continent_list, by = "country") %>% relocate(continent)

Result
Schermata 2022-10-05 alle 18.11.25

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.