Debian Codegraph @

R/dist_long.R

raw
1 # Copyright (C) Tal Galili
2 #
3 # This file is part of dendextend.
4 #
5 # dendextend is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # dendextend is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # A copy of the GNU General Public License is available at
16 # http://www.r-project.org/Licenses/
17 #
18
19 # Creating the "dendlist" class for more easily dealing with tanglegrams and so on
20
21
22 # Update the labels of a dist:
23
24 #' @export
25 `labels<-.dist` <- function(object, ..., value) {
26 attr(object, "Labels") <- value
27 object
28 }
29
30
31
32
33 Size.dist <- function(d) {
34 attr(d, "Size")
35 }
36
37
38
39 #' @title Turns a dist object to a "long" table
40 #' @description Turns a dist object from a "wide" to a "long" table
41 #' @export
42 #' @param d a distance object
43 #' @param ... not used
44 #' @return
45 #' A data.frame with two columns of rows and column names of the dist object
46 #' and a third column (distance) with the distance between the two.
47 #' @examples
48 #' data(iris)
49 #' iris[2:6, -5] %>%
50 #' dist() %>%
51 #' data.matrix()
52 #' iris[2:6, -5] %>%
53 #' dist() %>%
54 #' as.vector()
55 #' iris[2:6, -5] %>%
56 #' dist() %>%
57 #' dist_long()
58 #' # This can later be used to making a network plot based on the distances.
59 dist_long <- function(d, ...) {
60 n_seq <- seq_len(Size.dist(d))
61 rows_cols_grid <- expand.grid(rows = n_seq, cols = n_seq)
62 labels_grid <- expand.grid(rows = labels(d), cols = labels(d))
63 ss_keep <- rows_cols_grid[, 1] > rows_cols_grid[, 2]
64 # rows_cols[ss_keep,]
65 data.frame(labels_grid[ss_keep, ], distance = as.vector(d), stringsAsFactors = TRUE)
66 }