Beginner Question: How do collapse three ratio variables?

For my stats class, he wants us to collapse variables into new ones. I'm looking at precincts in the USA. Three of my variables are: %White officers (in this precinct), %Black officers, and %Hispanic Officers. How would I collapse a variable that would give which one was most predominant in a specific case? (Ex: 1 = %White is most predominant; 2 = %Black is most predominant; 3 = %Hispanic Officers is most predominant).

The example I was given was: mydata$VariableNewName= ifelse(mydata$Variable== 0, 1, mydata$VariableNewName).

But for this to work, instead of my ifelse statement equaling 0, it would have to equal whichever the predominant race is. Does that make sense? That's the part I'm stuck on.

For reference, this is how my SPSS Syntax worked if that makes my question any clearer:

Compute PredominantRace = 0.
If WhitePercent > BlackPercent AND WhitePercent > HispanicPercent PredominantRace = 1.
If BlackPercent > WhitePercent AND BlackPercent > HispanicPercent PredominantRace = 2.
If HispanicPercent > WhitePercent AND HispanicPercent > BlackPercent PredominantRace = 3.
Execute.

Let dat contain three numeric (double) variables, W, B and H. Create a new column D of character categorical variables WD, BD, HD, WB. WH, BH and WBH accounting for the possible cases.

dat %>% mutate(D = case_when(
                   W > B & W > H ~ WD,
                    ...
                    ))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.