Skip to contents

Transforms a square numeric distance matrix into a long-format tibble with customizable names for the row and column variables. Optionally, returns only unique row-column combinations (i.e., lower triangle).

Usage

tidy_dist_mat(
  d,
  rows_to = "Strain1",
  cols_to = "Strain2",
  unique_pairs = FALSE
)

Arguments

d

A square numeric matrix (or coercible to one) with row and column names.

rows_to

A string (length 1) naming the new column for row labels. Default is `"Strain1"`.

cols_to

A string (length 1) naming the new column for column labels. Default is `"Strain2"`.

unique_pairs

Logical (length 1). If `TRUE`, return only the unique row-column combinations from the lower triangle and diagonal of the matrix. Defaults to `FALSE`.

Value

A tibble in long format with columns:

[rows_to]

Factor, ordered by the row order of `d`

[cols_to]

Factor, ordered and reversed from column order of `d`

d

Distance value between row and column entries

Examples

mat <- matrix(1:9, nrow = 3)
rownames(mat) <- colnames(mat) <- c("A", "B", "C")
tidy_dist_mat(mat)
#> # A tibble: 9 × 3
#>   Strain1 Strain2     d
#>   <fct>   <fct>   <int>
#> 1 A       A           1
#> 2 A       B           4
#> 3 A       C           7
#> 4 B       A           2
#> 5 B       B           5
#> 6 B       C           8
#> 7 C       A           3
#> 8 C       B           6
#> 9 C       C           9
tidy_dist_mat(mat, unique_pairs = TRUE)
#> # A tibble: 6 × 3
#>   Strain1 Strain2     d
#>   <fct>   <fct>   <int>
#> 1 A       A           1
#> 2 B       A           2
#> 3 B       B           5
#> 4 C       A           3
#> 5 C       B           6
#> 6 C       C           9