I want to create a new variable "race" with 4 categories (Black/Latino/White/Others) in my dataset "data2" (see table below).
The conditions I want to apply are:
- Anyone indicating 'Hispanic or Latino' is coded as Latino, regardless if they indicated other options of race.
- Those who indicated 'Black' (only) would be coded Black
- Those who indicated 'White' (only) would be coded White
- Those who indicated 'Asian' or 'Native American', or 'others', OR indicated more than one option (unless Hispanic or Latino), would be coded Others
I want to use mutate
and case_when
function to create a new variable race following the condition above.
race_1 - Asian
race_2 - Black
race_3 - Hispanic or Latino
race_4 - Native American
race_5 - White
race_6 - others
race_1 | race_2 | race_3 | race_4 | race_5 | race_6 |
---|---|---|---|---|---|
NA | 1 | NA | NA | NA | NA |
NA | NA | 1 | NA | NA | NA |
NA | NA | 1 | NA | NA | NA |
1 | NA | 1 | NA | 1 | NA |
NA | NA | NA | NA | 1 | NA |
NA | NA | 1 | NA | NA | NA |