Reputation: 31
After running correlations I need to ID the row of the maximum value in each column. I am using which.max
but I can not get the row name. Instead I get an index number which is worthless. Each row has a name.
apply(my.data,2,which.max)
Upvotes: 3
Views: 9127
Reputation: 99331
The result from running correlations should be a matrix
, so here's an example using a matrix.
> M <- matrix(c(1,5,3,17,6,8,9,2,3,10,8,4), 4, 3)
> rownames(M) <- letters[1:4]
> M
## [,1] [,2] [,3]
## a 1 6 3
## b 5 8 10
## c 3 9 8
## d 17 2 4
> rownames(M)[apply(M, 2, which.max)]
## [1] "d" "c" "b"
Upvotes: 1
Reputation: 54237
# create example data
set.seed(1)
df <- data.frame(col1=runif(100), col2=runif(100))
row.names(df) <- paste0("row", 1:100)
# get max
rownames(df[apply(df, 2, which.max), ])
# [1] "row18" "row4"
Upvotes: 7