温馨提示:本文翻译自stackoverflow.com,查看原文请点击:r - Return column name for max function
max r

r - 返回max函数的列名

发布于 2020-04-10 23:48:01
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函数时,它返回最高的%(以数字表示)。我希望它返回种族匹配百分比最高的列的名称。

查看更多

提问者
user12801590
被浏览
8
akrun 2020-02-02 04:39

我们可以使用max.col返回每一行的最大值的列索引

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