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")