I have a matrix m
m <- matrix (
c( 2, 1, 8, 5,
7, 6, 3, 4,
9, 3, 2, 8,
1, 3, 7, 4),
nrow = 4,
ncol = 4,
byrow = TRUE)
rownames(m) <- c('A', 'B', 'C', 'D')
Now I would like to order the rows mbased on their respective distance, so I usedist()
dist_m <- dist(m)
dist_m when printing
A B C
B 8.717798
C 9.899495 5.477226
D 2.645751 7.810250 10.246951
Since I want it to be ordered, I try sort(dist_m), which prints
[1] 2.645751 5.477226 7.810250 8.717798 9.899495 10.246951
This is almost what I want. But I would be happier if he also printed the names of two lines, the number of which is the distance, something like
2.645751 A D
5.477226 B C
7.810250 B D
8.717798 A B
9.899495 A C
10.246951 C D
This, of course, is possible, but I have no idea how I could achieve this.