How do I get which.max to return the row name and not an index number

apply, r

Solution

# 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" 

Problem

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) ```

Original source