Collapse Single Nodes

Share:

Description

This function deletes the single nodes (i.e., with a single descendant) in a tree.

Usage

1
collapse.singles(tree, root.edge = FALSE)

Arguments

tree

an object of class "phylo".

root.edge

whether to get the singleton edges from the root until the first bifurcating node and put them as root.edge of the returned tree. By default, this is ignored or if the tree has no edge lengths (see examples).

Value

an object of class "phylo".

Author(s)

Emmanuel Paradis

See Also

plot.phylo, read.tree

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
## a tree with 3 tips and 3 nodes:
e <- c(4L, 6L, 6L, 5L, 5L, 6L, 1L, 5L, 3L, 2L)
dim(e) <- c(5, 2)
tr <- structure(list(edge = e, tip.label = LETTERS[1:3], Nnode = 3L),
                class = "phylo")
tr
## the following shows that node #4 (ie, the root) is a singleton
## and node #6 is the first bifurcating node
tr$edge
## Normally, a tree has less nodes than it has tips:
## > plot(tr)
## Error in plot.phylo(tr) :
##   there are single (non-splitting) nodes in your tree;
##   you may need to use collapse.singles()
collapse.singles(tr) # only 2 nodes
## give branch lengths to use the 'root.edge' option:
tr$edge.length <- runif(5)
str(collapse.singles(tr, TRUE)) # has a 'root.edge'

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.