Reputation: 103
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
Upvotes: 0
Views: 70
Reputation: 73315
input_matrix <- 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 = 4L,
dimnames = list(paste("area", 1:4, sep = "_"),
LETTERS[1:4]))
Looks like this is what you need:
DIAG <- diag(input_matrix); id <- which.max(DIAG)
matrix(c(rownames(input_matrix)[id], colnames(input_matrix)[id], DIAG[id]),
nrow = 1L, dimnames = list(NULL, c("Area", "Cat", "coeff")))
# Area Cat coeff
#[1,] "area_1" "A" "0.87"
Upvotes: 2
Reputation: 121077
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
Upvotes: 1