If you have data.frame, you can do something like:
df <- structure(list(A1 = c(0, 0, 0, 0), B1 = c(0.85, 0, 0, 0), C1 = c(0.45,
0.85, 0, 0), D1 = c(0.96, 0.56, 0.45, 0)), .Names = c("A1", "B1",
"C1", "D1"), row.names = c(NA, -4L), class = "data.frame")
data.frame( t(combn(names(df),2)), dist=t(df)[lower.tri(df)] )
X1 X2 dist
1 A1 B1 0.85
2 A1 C1 0.45
3 A1 D1 0.96
4 B1 C1 0.85
5 B1 D1 0.56
6 C1 D1 0.45
Another approach, if you use it as matrixwith + col line names, is to use it directly reshape2:
mat <- structure(c(0, 0, 0, 0, 0.85, 0, 0, 0, 0.45, 0.85, 0, 0, 0.96,
0.56, 0.45, 0), .Dim = c(4L, 4L), .Dimnames = list(c("A1", "B1",
"C1", "D1"), c("A1", "B1", "C1", "D1")))
library(reshape2)
subset(melt(mat), value!=0)
Var1 Var2 value
5 A1 B1 0.85
9 A1 C1 0.45
10 B1 C1 0.85
13 A1 D1 0.96
14 B1 D1 0.56
15 C1 D1 0.45