0

I have this column with a lot of countries.

countries <- c(Germany, France, Japan, China, Angola, Nigeria)

I want to create a new column called continent, agreggating these places. For example, I tried this but it didn't work:

if (countries == "Germany" | "France" {
   countries$continents <- "Europe"
 } else if (countries == "Japan"  |"China") {
   countries$continents <- "Asia"
 } else if (countries == "Angola"  |"Nigeria") {
   countries$continents <- "África" 

But R keep saying me i'm not allowed to compare strings. Maybe dplyr might have a clever solution, but any solution is welcomed. How I do this?

Jones
  • 333
  • 1
  • 11
  • 2
    This post might be helpful: [Get continent name from country name in R](https://stackoverflow.com/questions/47510141/get-continent-name-from-country-name-in-r) – markus Sep 18 '20 at 20:39
  • 1
    What you would need to use is `%in%`, e.g. `countries %in% c("Germany", "France")`. You compare a logical vector (`countries == "Germany"`) to a string `"France"`. – markus Sep 18 '20 at 20:40

1 Answers1

1

As @markus pointed out use %in%

You could try this using dplyr


library(dplyr)

df <- data.frame(countries = c("Germany", "France", "Japan", "China", "Angola", "Nigeria"))



df1 <- 
  df %>% 
  mutate(continent = case_when(countries %in% c("Germany", "France") ~ "Europe",
                               countries %in% c("Japan", "China") ~ "Asia",
                               countries %in% c("Angola", "Nigeria") ~ "Africa"))

But it might be neater to use the countrycode package, again as noted by @markus

library(countrycode)

df_continents <- 
  codelist %>% 
  select(country.name.en, continent)

df2 <- 
  df %>% 
  left_join(df_continents, by = c("countries" = "country.name.en"))

df2

#>   countries continent
#> 1   Germany    Europe
#> 2    France    Europe
#> 3     Japan      Asia
#> 4     China      Asia
#> 5    Angola    Africa
#> 6   Nigeria    Africa

Created on 2020-09-18 by the reprex package (v0.3.0)

Peter
  • 11,500
  • 5
  • 21
  • 31