返回max函数的列名 [英] Return column name for max function

查看:75
本文介绍了返回max函数的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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函数时,它返回最高的%(以数字表示).我希望它返回种族匹配百分比最高的列的名称.

Each one of the ethnicity categories returns a % likelihood of the person belonging to that ethnicity. When I use the pmax function, it returns the highest % (in numbers). I want it to return the name of the column with the ethnicity with the highest % match.

推荐答案

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

We can use max.col to return the index of the columns with the max value for each row

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

这篇关于返回max函数的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆