查找每个组的最大值并返回另一列 [英] Find max per group and return another column
本文介绍了查找每个组的最大值并返回另一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下测试矩阵:
testMatrix <- matrix( c(1,1,2,10,20,30,300,100,200,"A","B","C"), 3, 4)
colnames(testMatrix) <- c("GroupID", "ElementID", "Value", "Name")
在这里,我想找到每个组的最大值,然后返回该列的名称. 例如.我期望1,A和2,C.如果与max并列,则第一场比赛就可以了. 之后,我必须使用新的"GroupName"列将其附加到矩阵上
Here I want to find the max per group and then return the name of that column. E.g. I would expect 1, A and 2, C. If there is a tie with max, the first match would be fine. After that I would have to attach this to the matrix with a new Column "GroupName"
我该怎么做?
我已经有了组,最大值"组合:
I already have the Group, Max Value combination:
groupMax <- aggregate (as.numeric(testMatrix[,3]), by=list( testMatrix[,1] ), max )
我用来向矩阵中添加列的方式是这样的(假设已经有一个具有GroupID,Name组合的矩阵groupNames):
The way I used to add columns to my matrix works like this (let's assume there is also already a matrix groupNames with GroupID, Name combinations):
testMatrix <- cbind ( testMatrix, groupNames[match( testMatrix[,1], groupNames[,1] ), 2] )
推荐答案
基本解决方案,不如Dan M的简单:
Base solution, not as simple as Dan M's:
testMatrix <- data.frame(GroupID = c(1,1,2), ElementID = c(10,20,30),
Value=c(300,100,200), Name=c("A","B","C"))
A <- lapply(split(testMatrix, testMatrix$GroupID), function(x) {
x[which.max(x$Value), c(1, 4)]
}
)
do.call(rbind, A)
这篇关于查找每个组的最大值并返回另一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文