2

我想知道对于给定的深度截止,dendrogram我可以为低于该深度截止的每个分支获取作为其后代的所有叶子名称的列表的方式是什么。

例如我创建这个dendrogram

set.seed(1)
mat <- matrix(rnorm(100*10),nrow=100,ncol=10)
dend <- as.dendrogram(hclust(dist(t(mat))))

绘制它使用dendextend

require(dendextend)
dend %>% plot

并将深度截止定义为 14.5:

abline(h=14.5,col="red")

在此处输入图像描述

我的清单应该是:

list(c(5),c(7),c(8),c(10,4,9),c(3,6,1,2))
4

2 回答 2

1
set.seed(1)
mat <- matrix(rnorm(100*10),nrow=100,ncol=10)
dend <- as.dendrogram(hclust(dist(t(mat))))

require(dendextend)
dend %>% plot
abline(h=14.5,col="red")

cutree functionindendextend接受一个高度截止值,并将返回一个vector具有组成员资格的整数:

> cutree(dend,h=14.5)
 1  2  3  4  5  6  7  8  9 10 
 1  1  1  2  3  1  4  5  2  2 
于 2017-01-08T21:35:20.017 回答
0

不完全确定这是否是您想要的答案,但您可以像这样访问它们吗?

acme$Accounting$children %>% names()
"New Software"             "New Accounting Standards"

acme$IT$children %>% names()
"Outsource"   "Go agile"    "Switch to R"

大概您想自动执行此操作,因此它将类似于

names = c('Accounting', 'IT')
sapply(names, function(x) acme[[x]]$children %>% names(.))

我认为可能有一种更优雅的方式来做到这一点,但这看起来并不是一种糟糕的方式。

编辑

由于用户完全改变了这里的问题,这里是一个新的答案:

get_height = function(x){
  a = attributes(x)
  a$height
}

height = 14
dendrapply(dend, function(x) ifelse(get_height(x) < height, x, '')) %>% unlist()

您只需要访问树状图中每个终端节点的高度,并确定它是高于还是低于您想要的高度。不幸的是,这不会将来自同一父节点的叶节点组合在一起 - 但是,通过一些修补来添加它应该不会太难。希望这能让你上路。

于 2017-01-08T11:37:04.917 回答