Joel B Joel B - 3 months ago 9
R Question

storing the rowname column name and cell for the max in a matrix diagional

I have a matrix of correlation coefficients (called

input_matrix
) which looks like the following and shows the correlation coefficient of multiple areas (1,2,3,4) against multiple categories (A,B,C,D) (NB, this is a smaller version with the same format & the areas are row name rather than a column):

| A | B | C | D
------ | ------ | ------ | ------ | ------
area_1 | 0.870 | 0.435 | 0.968 | 0.679
area_2 | 0.456 | 0.259 | 0.906 | 0.467
area_3 | 0.298 | 0.256 | 0.457 | 0.768
area_4 | 0.994 | 0.987 | 0.365 | 0.765


I wanted to know how to find the maximum coefficient from the matrix diagonal and to store the column and row name info next to it (like in the desired output below). I have found the
diag2vec()
function which works to find the max value on the diagonal but by default it removes the row name (my area info) and column name (my category info):

Desired Output (output_matrix)

Area | Cat | coeff
------ | ------ | ------
area_1 | A | 0.870

Answer

Here is your data:

(x <- matrix(
  c(0.870, 0.435, 0.968, 0.679, 
    0.456, 0.259, 0.906, 0.467, 
    0.298, 0.256, 0.457, 0.768, 
    0.994, 0.987, 0.365, 0.765),
  nrow = 4,
  dimnames = list(area = 1:4, category = letters[1:4])
))
##     category
## area     a     b     c     d
##    1 0.870 0.456 0.298 0.994
##    2 0.435 0.259 0.256 0.987
##    3 0.968 0.906 0.457 0.365
##    4 0.679 0.467 0.768 0.765

This is the diagonal, and the position of its maximum:

diag_x <- diag(x)
i <- which.max(diag_x)

Here are the values that you want:

data.frame(
  area     = rownames(x)[i],
  category = colnames(x)[i],
  coeff    = x[i, i]
)
##   area category coeff
## 1    1        a  0.87