Skip to content

Instantly share code, notes, and snippets.

@aaronwolen
Created November 12, 2014 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronwolen/c5084e5e7b1f16447789 to your computer and use it in GitHub Desktop.
Save aaronwolen/c5084e5e7b1f16447789 to your computer and use it in GitHub Desktop.
Identify clusters of contiguous factors within a dendrogram
# Identify clusters of contiguous factors within a dendrogram
# Aaron Wolen
#
# See http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0038422
# for an example situation in which this would be useful
#
# x: dendrogram
# f: a named vector containing the factor that defines the grouping of leaves,
# must be named using the same values that define x's leaf labels
contigs <- function(x, f) {
UseMethod("contigs", x)
}
contigs.dendrogram <- function(x, f) {
if(is.null(names(f))) stop("f must be a named vector.")
# obtain ordered leaf labels
leaves <- unlist(dendrapply(x, attr, which = "label"))
groups <- f[leaves]
# identify clusters
cluster <- rle(groups)
clustered <- cluster$values[cluster$lengths > 1]
clustered <- rep(cluster$length, cluster$length) > 1
groups[!clustered] <- NA
return(groups)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment