| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/attr_access.R |
| 3 |
\name{get_nodes_attr} |
| 4 |
\alias{get_nodes_attr} |
| 5 |
\title{Get attributes of dendrogram's nodes} |
| 6 |
\source{ |
| 7 |
Heavily inspired by the code in the |
| 8 |
function \code{labels.dendrogram}, |
| 9 |
so credit should go to Martin Maechler. |
| 10 |
} |
| 11 |
\usage{ |
| 12 |
get_nodes_attr( |
| 13 |
dend, |
| 14 |
attribute, |
| 15 |
id, |
| 16 |
include_leaves = TRUE, |
| 17 |
include_branches = TRUE, |
| 18 |
simplify = TRUE, |
| 19 |
na.rm = FALSE, |
| 20 |
... |
| 21 |
) |
| 22 |
} |
| 23 |
\arguments{ |
| 24 |
\item{dend}{a dendrogram object} |
| 25 |
|
| 26 |
\item{attribute}{character scalar of the attribute (\code{attr}) |
| 27 |
we wish to get from the nodes} |
| 28 |
|
| 29 |
\item{id}{integer vector. If given - only the attr of these nodes id will be returned (via depth first search)} |
| 30 |
|
| 31 |
\item{include_leaves}{logical. Should leaves attributes be included as well?} |
| 32 |
|
| 33 |
\item{include_branches}{logical. Should non-leaf (branch node) |
| 34 |
attributes be included as well?} |
| 35 |
|
| 36 |
\item{simplify}{logical (default is TRUE). should the result be simplified |
| 37 |
to a vector (using \link{simplify2array} ) if possible? If it is not possible |
| 38 |
it will return a matrix. When FALSE, a list is returned.} |
| 39 |
|
| 40 |
\item{na.rm}{logical. Should NA attributes be REMOVED from the resulting vector?} |
| 41 |
|
| 42 |
\item{...}{not used} |
| 43 |
} |
| 44 |
\value{ |
| 45 |
A vector with the dendrogram's nodes attribute. If an attribute is missing |
| 46 |
from some nodes, it will return NA in that vector. |
| 47 |
} |
| 48 |
\description{ |
| 49 |
Allows easy access to attributes of branches and/or leaves, with option |
| 50 |
of returning a vector with/withough NA's (for marking the missing attr value) |
| 51 |
} |
| 52 |
\examples{ |
| 53 |
# define dendrogram object to play with: |
| 54 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 55 |
dend <- as.dendrogram(hc) |
| 56 |
|
| 57 |
# get_leaves_attr(dend) # error :) |
| 58 |
get_leaves_attr(dend, "label") |
| 59 |
labels(dend, "label") |
| 60 |
get_leaves_attr(dend, "height") # should be 0's |
| 61 |
get_nodes_attr(dend, "height") |
| 62 |
|
| 63 |
|
| 64 |
get_leaves_attr(dend, "leaf") # should be TRUE's |
| 65 |
get_nodes_attr(dend, "leaf") # conatins NA's |
| 66 |
|
| 67 |
|
| 68 |
get_leaves_attr(dend, "members") # should be 1's |
| 69 |
get_nodes_attr(dend, "members", include_branches = FALSE, na.rm = TRUE) # |
| 70 |
get_nodes_attr(dend, "members") # |
| 71 |
get_nodes_attr(dend, "members", simplify = FALSE) |
| 72 |
get_nodes_attr(dend, "members", include_leaves = FALSE, na.rm = TRUE) # |
| 73 |
|
| 74 |
get_nodes_attr(dend, "members", id = c(1, 3), simplify = FALSE) |
| 75 |
get_nodes_attr(dend, "members", id = c(1, 3)) # |
| 76 |
|
| 77 |
|
| 78 |
hang_dend <- hang.dendrogram(dend) |
| 79 |
get_leaves_attr(hang_dend, "height") # no longer 0! |
| 80 |
get_nodes_attr(hang_dend, "height") # does not include any 0s! |
| 81 |
|
| 82 |
# does not include leaves values: |
| 83 |
get_nodes_attr(hang_dend, "height", include_leaves = FALSE) |
| 84 |
# remove leaves values all together: |
| 85 |
get_nodes_attr(hang_dend, "height", include_leaves = FALSE, na.rm = TRUE) |
| 86 |
\dontrun{ |
| 87 |
library(microbenchmark) |
| 88 |
# get_leaves_attr is twice faster than get_nodes_attr |
| 89 |
microbenchmark( |
| 90 |
get_leaves_attr(dend, "members"), # should be 1's |
| 91 |
get_nodes_attr(dend, "members", include_branches = FALSE, na.rm = TRUE) |
| 92 |
) |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
\seealso{ |
| 97 |
\link{get_leaves_attr}, \link{nnodes}, \link{nleaves} |
| 98 |
} |