Joel B - 1 year ago 66

R Question

I have a matrix of correlation coefficients (called

`input_matrix`

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

`Area | Cat | coeff`

------ | ------ | ------

area_1 | A | 0.870

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**