Creating race_ethn column based on logic involving two columns

I had a question on the ifelse function. I am trying to create a new column (race_ethn) which is the combination of the Race column and Ethnicity column based on the following logic:

If Ethnicity = Hispanic or Latino, then we want to keep the value in the Ethnicity column (which is Hispanic or Latino, ALL ELSE, we want to keep the value in the Race column.

I tried using this code below in my pipeline and keep getting an error. I am assuming it is because I am using columns instead of operators.

mutate(race_ethn = ifelse(Ethnicity = “Hispanic or Latino”, Ethnicity, Race))

I attached a sample dataset to help solve this inquiry.

Thanks in advanced.
Sample Data.txt (322 Bytes)

2 Likes

Hello @wilma.figueroa, try using == instead of = inside the ifelse function.

Like this: mutate(race_ethn = ifelse(Ethnicity == "Hispanic or Latino", Ethnicity, Race))

In R, == tests if two values are equal and returns TRUE or FALSE, while = is used for assignment.

I hope it helped.

4 Likes

Success! Thank you so much! :blush:

1 Like