对于每一行,返回最大值的列名 [英] For each row return the column name of the largest value

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

问题描述

我有一个员工名册,我需要知道他们最常在哪个部门工作.将员工 ID 与部门名称制成表格是微不足道的,但从频率表中返回部门名称而不是名册计数则更棘手.下面是一个简单的例子(列名 = 部门,行名 = 员工 ID).

I have a roster of employees, and I need to know at what department they are in most often. It is trivial to tabulate employee ID against department name, but it is trickier to return the department name, rather than the number of roster counts, from the frequency table. A simple example below (column names = departments, row names = employee ids).

DF <- matrix(sample(1:9,9),ncol=3,nrow=3)
DF <- as.data.frame.matrix(DF)
> DF
  V1 V2 V3
1  2  7  9
2  8  3  6
3  1  5  4

现在我如何获得

> DF2
  RE
1 V3
2 V1
3 V2

推荐答案

使用您的数据的一个选项(以供将来参考,使用 set.seed() 使用 sample 制作示例代码>可重现):

One option using your data (for future reference, use set.seed() to make examples using sample reproducible):

DF <- data.frame(V1=c(2,8,1),V2=c(7,3,5),V3=c(9,6,4))

colnames(DF)[apply(DF,1,which.max)]
[1] "V3" "V1" "V2"

比使用 apply 更快的解决方案可能是 max.col:

A faster solution than using apply might be max.col:

colnames(DF)[max.col(DF,ties.method="first")]
#[1] "V3" "V1" "V2"

...其中 ties.method 可以是 "random" "first""last"

...where ties.method can be any of "random" "first" or "last"

如果您碰巧有两列等于最大值,这当然会导致问题.我不确定在这种情况下您想做什么,因为某些行将有多个结果.例如:

This of course causes issues if you happen to have two columns which are equal to the maximum. I'm not sure what you want to do in that instance as you will have more than one result for some rows. E.g.:

DF <- data.frame(V1=c(2,8,1),V2=c(7,3,5),V3=c(7,6,4))
apply(DF,1,function(x) which(x==max(x)))

[[1]]
V2 V3 
 2  3 

[[2]]
V1 
 1 

[[3]]
V2 
 2 

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

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