cake2244
cake2244

Reputation: 29

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

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!

Upvotes: 0

Views: 42

Answers (1)

jdharden
jdharden

Reputation: 11

You could also use unite in the tidyr package.

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

Upvotes: 0

Related Questions