我想知道在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))发布于 2017-01-09 05:35:20
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")dendextend中的cutree function接受高度截止值,并将返回一个带有组成员身份的整数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-08 19:37:05
不完全确定这是否是你想要的答案,但你能像这样访问它们吗?
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()您只需要访问树状图中每个末端节点的高度,并确定它是高于还是低于您希望的高度。不幸的是,这不会将来自同一父节点的叶节点分组在一起--然而,这应该不会太困难,只需进行一些修补即可。希望这能让你上路。
https://stackoverflow.com/questions/41531551
复制相似问题