Given the following test matrix:
testMatrix <- matrix( c(1,1,2,10,20,30,300,100,200,"A","B","C"), 3, 4) colnames(testMatrix) <- c("GroupID", "ElementID", "Value", "Name")
Here I want to find max for each group, and then return the name of this column. For example. I would expect 1, A and 2, C. If there is a tie with max, the first match will be perfect. After that I would have to attach this to the matrix with a new column "GroupName"
How can i do this?
I already have a combination of Group, Max Value:
groupMax <- aggregate (as.numeric(testMatrix[,3]), by=list( testMatrix[,1] ), max )
The way I used to add columns to my matrix works like this (suppose there is also a matrix groupNames with GroupID, name combinations):
testMatrix <- cbind ( testMatrix, groupNames[match( testMatrix[,1], groupNames[,1] ), 2] )