1

This is a follow-up to an earlier question: Return column name for max function

ethnic <- c("white", "black", "hispanic", "asian", "other")
ethnicity$ethnicity  <- ethnic[max.col(ethnicity[ethnic], 'first')]

This code returns the ethnicity of each individual based on the ethnic category with the highest proportion. That is great.

However, I want to take it a step further. Instead of returning the ethnic group with the highest proportion, I want it to return the ethnic group over 0.8. The difference is, if the ethnic category with the highest (max) proportion is below 0.8, it returns "No Match".

E.g.

John:
white  black  hispanic  asian  other
0.5.   0.2    0.1       0.2    0.0

This should return No Match.

Jack:
white  black  hispanic  asian  other
0.8    0.1    0.0       0.1    0.0

This should return White.

Here is a reproducible sample using dput():

ethnicity <- structure(list(year = c(2010L, 2013L, 2009L, 2014L, 2001L), property = c("6446 025", 
"6710 034", "0525 065", "0272 006", "1720 030"), address = c("1147 NAPLES ST", 
"73 MIZPAH ST", "43 ESTATES CT", "650 BUSH ST", ""), city = c("SAN FRANCISCO CA", 
"SAN FRANCISCO CA", "SAN RAFAEL CA", "SAN FRANCISCO CA", ""), 
    city_overflow = c("", "", "", "", ""), zip = c("94112", "94131", 
    "94901", "94108", ""), surname = c("DELEON", "HENDERSON", 
    "KOORHAN", "EXECUTIVE", "WONG"), name = c("ESTELA", "DANIEL", 
    "GLENN", "HOTEL", "CHUN"), middle = c(NA, "V ", "S", "VINTAGE COURT", 
    NA), Owner2 = c("GAMEZ JUAN", " HELENE E", NA, NA, NA), Owner3 = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), org = c(FALSE, FALSE, FALSE, FALSE, FALSE), surname.match = c("DELEON", 
    "HENDERSON", "", "", "WONG"), white = c(0.0664, 0.5963, 0.6665, 
    0.6665, 0.0348), black = c(0.0104, 0.3398, 0.0853, 0.0853, 
    0.0079), hispanic = c(0.8306, 0.0251, 0.1367, 0.1367, 0.04
    ), asian = c(0.0831, 0.0045, 0.0797, 0.0797, 0.8649), other = c(0.0095, 
    0.0342, 0.0318, 0.0318, 0.0524), ethnicity = c("hispanic", 
    "white", "white", "white", "asian")), row.names = c("1998", 
"3431", "6884", "39310", "9524"), class = "data.frame")
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
  • 2
    Please add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! – dario Feb 09 '20 at 15:19

2 Answers2

2

To continue the original solution, I add a new column which has value 0.8. Then max.col can find the column indices of maximum values. If the max is 0.8, then it'll return the index of "No_Match".

original solution

ethnic[max.col(ethnicity[ethnic], 'first')]

# [1] "hispanic" "white"    "white"    "white"    "asian"   

adjusted solution

c(ethnic, "No_Match")[max.col(cbind(ethnicity[ethnic], 0.8), 'first')]

# [1] "hispanic" "No_Match" "No_Match" "No_Match" "asian"
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
2

Here is an answer with base R.

ethnicity$ethnicity <- apply(ethnicity[14:18], 1, function(x) {
  i <- x >= 0.8
  if(any(i)) ethnic[i] else "No Match"
})
ethnicity
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66