ethnicity_col_names <- c("surname", "first_name", "surname.match", "white", "black",
"hispanic", "asian", "other")
colnames(ethnicity_sample) <- ethnicity_col_names
ethnicity_sample$try <- pmax(ethnicity_sample$white, ethnicity_sample$black, ethnicity_sample$hispanic,
ethnicity_sample$asian, ethnicity_sample$other)
每个种族类别都会返回该种族所属人士的可能性的百分比。当我使用pmax函数时,它返回最高的%(以数字表示)。我希望它返回种族匹配百分比最高的列的名称。
我们可以使用max.col
返回每一行的最大值的列索引
nm1 <- c("white", "black", "hispanic", "asian", "other")
ethnicity_sample$try <- nm1[max.col(ethnicity_sample[nm1], 'first')]
难以置信-非常感谢!!!