2

I'm redoing some old university essays to convert it from Stata to R to learn R, and am trying to find a way to take a variable of countries, and create a new variable with a factor of being an OECD member or Non-member.

I have tried to mutate from dplyr with an if-else statement, but it does not seem to work, and all the googling I have tried seem to just show releveling existing factors and such.

As an example (country_name contains names of all the countries in the world, shortended for ease of readability):

df <- data.frame(country_name = c("Australia", "Austria", "Belgium", "Algeria", "Bahrain", "Comoros"))

df <- df %>% mutate(OECD = ifelse(country_name = c("Australia", "Austria", "Belgium"), "OECD", "Non-OECD")

I would have expected that the row containing "Australia", "Austria", "Belgium" would have gotten the factor level of OECD, while "Algeria", "Bahrain", "Comoros" would have gotten Non-OECD.

Ps, as this is my second question ever, any tips on how to improve my question making are much appreciated!

SOLVED: As pointed out in the accepted answer below, I should have used the %in% operator. Many thanks!

  • 2
    you need `%in%` do `df %>% mutate(OECD = ifelse(country_name %in% c("Australia", "Austria", "Belgium"), "OECD", "Non-OECD"))` Or directly `df$OECD <- ifelse(df$country_name %in% c("Australia", "Austria", "Belgium"), "OECD", "Non-OECD")` – Ronak Shah Aug 07 '19 at 09:58

2 Answers2

1

You need to use the %in%.

If you want to remain within the tidyverse framework then:

df %>% 
  mutate(OECD = ifelse(df[,"country_name"] %in% c("Australia", "Austria", "Belgium"), "OECD", "Non-OECD"))

Or for a base R way:

df[,"OECD"] <- ifelse(df[,"country_name"] %in% c("Australia", "Austria", "Belgium"), "OECD", "Non-OECD")

Or data.table way (load data.table package and use data.table() instead of data.frame())

df[, OECD := "Non-OECD"][country_name %in% c("Australia", "Austria", "Belgium"), OECD := "OECD"]

This data.table method is making a new variable where all are "Non-OECD" then converting specific ones to "OECD" - I'm just learning data.table myself and really liking it.

rg255
  • 4,119
  • 3
  • 22
  • 40
  • Thank you! And @Ronak Shah who gave the same answer as a comment, this worked perfectly! – Leo Carlsson Aug 07 '19 at 10:13
  • No worries, just added a base R and data.table version too -as a general comment, if you're new to R you should consider using tools from more than just the tidyverse, tidyverse is a toolbox with a lot of (very good) tools, but sometimes there's toolbox with tools more suited to the task. – rg255 Aug 07 '19 at 10:19
0

You are close. Just missing the "==" and a ")"

df <- df %>% mutate(OECD = ifelse(country_name == c("Australia", "Austria", "Belgium"), "OECD", "Non-OECD"))
Dominix
  • 433
  • 3
  • 9
  • Thanks! This however had problem with the dataset I worked with, since it contained Australia several times, and only marked every third of them as OECD. Something I now realise I should have mentioned in my question! =) – Leo Carlsson Aug 07 '19 at 10:17