【问题标题】:Return column name for max function返回 max 函数的列名
【发布时间】:2020-02-01 20:38:22
【问题描述】:
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 函数时,它返回最高的百分比(以数字表示)。我希望它返回具有最高匹配百分比的种族的列的名称。

【问题讨论】:

    标签: r max


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-16
      • 2017-03-20
      • 2017-08-24
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      相关资源
      最近更新 更多