1

Suppose I have a distance matrix "m":

         0       1       2       4       5       6       7       8       9      10
0  0.00000 1.68980 7.55815 4.18765 4.48060 4.41775 3.97950 4.12830 4.25500 4.48110
1  1.68980 0.00000 7.67225 4.11300 4.48225 4.62525 3.92880 4.02495 4.19675 4.46860
2  7.55815 7.67225 0.00000 7.31290 7.23675 7.46935 7.29925 7.41055 7.43290 7.28585
4  4.18765 4.11300 7.31290 0.00000 3.81510 3.35225 2.88600 3.29000 3.01940 3.94900
5  4.48060 4.48225 7.23675 3.81510 0.00000 4.29490 3.66205 4.00220 3.70005 2.34825
6  4.41775 4.62525 7.46935 3.35225 4.29490 0.00000 3.42355 3.63880 2.27245 4.23745
7  3.97950 3.92880 7.29925 2.88600 3.66205 3.42355 0.00000 2.48115 2.97045 3.61370
8  4.12830 4.02495 7.41055 3.29000 4.00220 3.63880 2.48115 0.00000 2.97630 3.92015
9  4.25500 4.19675 7.43290 3.01940 3.70005 2.27245 2.97045 2.97630 0.00000 3.80345
10 4.48110 4.46860 7.28585 3.94900 2.34825 4.23745 3.61370 3.92015 3.80345 0.00000

I would like to know which col/row names cause values above 5.

I tried:

> m[which(m > 5)]
 [1] 7.55815 7.67225 7.55815 7.67225 7.31290 7.23675 7.46935 7.29925 7.41055 7.43290 7.28585 7.31290 7.23675 7.46935 7.29925 7.41055
[17] 7.43290 7.28585

But this returns only the values.

When I try:

> colnames(m[which(m > 5)])

I get NULL

In this example, I am trying to catch "2" which responsible for values above 5.

0

2 Answers 2

2

If you use which with arr.ind = TRUE it gives row as well as column number where values are greater than 5.

which(mat > 5, arr.ind = TRUE)

#   row col
#2    3   1
#2    3   2
#0    1   3
#4    4   3
#5    5   3
#6    6   3
#7    7   3
#...

To get row and column names you can extract first and second column respectively.

rownames(mat)[which(mat > 5, arr.ind = TRUE)[, 1]]
colnames(mat)[which(mat > 5, arr.ind = TRUE)[, 2]]
Sign up to request clarification or add additional context in comments.

Comments

0

If we are interested in the row/column names, then convert to table, and coerce it to a data.frame and subset

subset(as.data.frame(as.table(m)), Freq > 5, select = c(Var1, Var2))

data

m <-structure(c(0, 1.6898, 7.55815, 4.18765, 4.4806, 4.41775, 3.9795, 
4.1283, 4.255, 4.4811, 1.6898, 0, 7.67225, 4.113, 4.48225, 4.62525, 
3.9288, 4.02495, 4.19675, 4.4686, 7.55815, 7.67225, 0, 7.3129, 
7.23675, 7.46935, 7.29925, 7.41055, 7.4329, 7.28585, 4.18765, 
4.113, 7.3129, 0, 3.8151, 3.35225, 2.886, 3.29, 3.0194, 3.949, 
4.4806, 4.48225, 7.23675, 3.8151, 0, 4.2949, 3.66205, 4.0022, 
3.70005, 2.34825, 4.41775, 4.62525, 7.46935, 3.35225, 4.2949, 
0, 3.42355, 3.6388, 2.27245, 4.23745, 3.9795, 3.9288, 7.29925, 
2.886, 3.66205, 3.42355, 0, 2.48115, 2.97045, 3.6137, 4.1283, 
4.02495, 7.41055, 3.29, 4.0022, 3.6388, 2.48115, 0, 2.9763, 3.92015, 
4.255, 4.19675, 7.4329, 3.0194, 3.70005, 2.27245, 2.97045, 2.9763, 
0, 3.80345, 4.4811, 4.4686, 7.28585, 3.949, 2.34825, 4.23745, 
3.6137, 3.92015, 3.80345, 0), .Dim = c(10L, 10L), .Dimnames = list(
    c("0", "1", "2", "4", "5", "6", "7", "8", "9", "10"), c("0", 
    "1", "2", "4", "5", "6", "7", "8", "9", "10")))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.