Creating a new variable in R that merges categorical entry options of two variables

39 views Asked by At

So I have one variable in my R dataset called "RACE" with different categorical race classifications. I also have another variable called "ETHNICITY" with different categorical ethnicity classifications. I want to create a new variable (new column) in my dataset called "RACE/ETHNICITY" that merges these two. For instance, if RACE is "Black" and Ethnicity is "Non-Hispanic" the corresponding entry under RACE/ETHNICITY column would be "Black Non-Hispanic."

What is the best way to go about this in R? Any help and sample code would be appreciated!

1

There are 1 answers

0
jdharden On

You could also use unite in the tidyr package.

df |>
unite(race_eth, c("RACE","ETHNICITY"), sep = " ", remove = F)