0

I have just started to learn R and I am trying to create a new variable + using ifelse.

   nlsy$ethnicity <- as.factor(ifelse(nlsy$race_ethnicity=="Black", "Black",
  ifelse(nlsy$race_ethnicity=="Hispanic", "Hispanic",
  ifelse(nlsy$race_ethnicity=="Non-Black / Non-Hispanic" & nlsy$race_ethnicity="White", "White", "Other"))))




5
  • Use case_when from dplyr to avoid nested ifelse statements, but it would also be good to see some sample data to better understand what you are trying to do here. Commented Feb 4, 2021 at 3:22
  • What exactly is the issue in the code that you have tried here? Commented Feb 4, 2021 at 3:25
  • I get the error: Error: unexpected '=' in: " ifelse(nlsy$race_ethnicity=="Hispanic", "Hispanic", ifelse(nlsy$race_ethnicity=="Non-Black / Non-Hispanic" & nlsy$race=" Commented Feb 4, 2021 at 3:28
  • 1
    That is because you have last condition as nlsy$race_ethnicity="White", which actually should be nlsy$race_ethnicity== "White". Also maybe the condition should be | (OR) rather than & (AND). Commented Feb 4, 2021 at 3:31
  • I believe Ronak is correct. Commented Feb 4, 2021 at 3:40

3 Answers 3

2

Use case_when from the dplyr package here:

nlsy$ethnicity <- case_when(
    nlsy$race_ethnicity == "Black" ~ "Black",
    nlsy$race_ethnicity == "Hispanic" ~ "Hispanic",
    nlsy$race_ethnicity == "Non-Black / Non-Hispanic" | nlsy$race_ethnicity == "White" ~ "White",
    TRUE ~ "Other
)

Using case_when is often preferable to ifelse especially in the case where the latter involves many nested calls.

Sign up to request clarification or add additional context in comments.

Comments

1

We could make this a bit more simpler with

library(dplyr)
nlsy <- nlsy %>%
     mutate(ethnicity = replace(replace(race_ethnicity, 
      race_ethnicity == "Non-Black / Non-Hispanic", "White"), 
        !race_ethnicity %in% c("Black", "Hispanic", "White"), "Other"))

Comments

0

There is also a package called fmtr that is good for this situation. It has a value() function, which is similar to a SAS user-defined format. Here is how you use it:

library(fmtr)

# Define format
fmt <- value(condition(x == "Black", "Black"),
             condition(x == "Hispanic", "Hispanic"),
             condition(x == "Non-Black / Non-Hispanic" & x == "White", "White"),
             condition(TRUE, "Other")

# Apply format      
nlsy$ethnicity <- fapply(nlsy$race_ethnicity, fmt)


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.