If you want the top 5% of values in the matrix, here is one way to do it:
# Fake data
set.seed(2)
x = matrix(rnorm(100), nrow=10)
rownames(x) = colors()[seq(1,100, length=10)]
colnames(x) = LETTERS[1:10]
round(x, 2)
A B C D E F G H I J
white -0.90 0.42 2.09 0.74 -0.38 -0.84 -1.79 -0.92 1.00 1.60
aquamarine4 0.18 0.98 -1.20 0.32 -1.96 2.07 2.03 0.33 -1.70 1.68
bisque4 1.59 -0.39 1.59 1.08 -0.84 -0.56 -0.70 -0.14 -0.53 -1.18
brown2 -1.13 -1.04 1.95 -0.28 1.90 1.28 0.16 0.43 -1.37 -1.36
cadetblue3 -0.08 1.78 0.00 -0.78 0.62 -1.05 0.51 -0.05 -2.21 -1.51
chocolate4 0.13 -2.31 -2.45 -0.60 1.99 -1.97 -0.82 -0.91 1.82 -1.25
cornsilk4 0.71 0.88 0.48 -1.73 -0.31 -0.32 -2.00 1.30 -0.65 1.96
darkgoldenrod3 -0.24 0.04 -0.60 -0.90 -0.09 0.94 -0.48 0.77 -0.28 0.01
darkolivegreen4 1.98 1.01 0.79 -0.56 -0.18 1.14 0.08 1.05 -0.39 -0.84
darkred -0.14 0.43 0.29 -0.25 -1.20 1.67 -0.90 -1.41 0.39 -0.60
Return the top 5% of values in the matrix:
x[x > quantile(x, prob=0.95)]
[1] 1.984474 2.090819 1.990920 2.066301 2.031243
Return the indices of the top 5% of values:
idx = which(x > quantile(x, prob=0.95), arr.ind=TRUE)
idx
row col
darkolivegreen4 9 1
white 1 3
chocolate4 6 5
aquamarine4 2 6
aquamarine4 2 7
x[idx]
[1] 1.984474 2.090819 1.990920 2.066301 2.031243
If you want to keep the values and the index names together you could do:
idx.names = cbind(as.data.frame(idx),
rownames=rownames(x)[idx[,1]],
colnames=colnames(x)[idx[,2]])
rownames(idx.names) = 1:nrow(idx.names)
idx.names = cbind(idx.names, values=x[as.matrix(idx.names[, 1:2])])
idx.names
row col rownames colnames values
1 9 1 darkolivegreen4 A 1.984474
2 1 3 white C 2.090819
3 6 5 chocolate4 E 1.990920
4 2 6 aquamarine4 F 2.066301
5 2 7 aquamarine4 G 2.031243