This function merges distance object(s) into a single data frame which rows
are pairs of elements and column(s) distance metric(s). It stands on the
dist_long
function.
dist.to.df(list_dist)
a list of dist object(s). All dist objects should have a name (e.g. name of distance metric) and the same labels (i.e. names of sets between which distance was computed).
A data frame which first and second columns (names x1
and x2
)
contain names of the 2 sets involved in each pair, and with one column for
each dist object (named after its name in list_dist
.
# Create dist objects:
dist_A <- round(dist(matrix(runif(10, 0, 1), 5, 2,
dimnames = list(letters[1:5], NULL))), 2)
dist_B <- round(dist(matrix(runif(10, 0, 1), 5, 2,
dimnames = list(letters[1:5], NULL))), 2)
dist_C <- round(dist(matrix(runif(10, 0, 1), 5, 2,
dimnames = list(letters[1:5], NULL))), 2)
# First example with only 1 distance:
dist.to.df(list(dA = dist_A))
#> x1 x2 dA
#> 1 a b 0.55
#> 2 a c 0.36
#> 3 a d 0.87
#> 4 a e 0.68
#> 5 b c 0.61
#> 6 b d 0.45
#> 7 b e 0.28
#> 8 c d 0.71
#> 9 c e 0.56
#> 10 d e 0.20
# Second example with 3 distances:
dist.to.df(list(d1 = dist_A, d2 = dist_B, d3 = dist_C))
#> x1 x2 d1 d2 d3
#> 1 a b 0.55 0.32 0.88
#> 2 a c 0.36 0.61 0.61
#> 3 a d 0.87 0.50 0.45
#> 4 a e 0.68 0.67 0.44
#> 5 b c 0.61 0.30 0.53
#> 6 b d 0.45 0.36 0.43
#> 7 b e 0.28 0.50 0.54
#> 8 c d 0.71 0.33 0.35
#> 9 c e 0.56 0.38 0.19
#> 10 d e 0.20 0.16 0.20