2

大概很简单,

我有一个dendrogram

set.seed(1)
my_mat <- matrix(rnorm(100),nrow=10,ncol=10)
my_dend <- as.dendrogram(hclust(dist(my_mat)))

我想用来从每个in 中dendrapply提取,因为它遍历in 。height attributenodemy_denddendrogrampre-order

尝试dendrapply例子my_dend:_

dendrapply(my_dend, function(n) utils::str(attributes(n)))

它不返回值,而是打印我需要的信息pre-order。我认为只是得到height attribute返回很简单:

dendrapply(my_dend, function(n) attr(n,"height"))

但显然我错了。

任何想法?

4

3 回答 3

2

这是你想要的吗?

sapply(hclust(dist(my_mat)), '[')$height
#[1] 2.195193 2.661372 2.837259 2.890944 3.745600 4.098533 4.177088 5.514541 6.496675
#and order
sapply(hclust(dist(my_mat)), '[')$order
# [1]  4  1 10  8  9  2  5  7  3  6

dendextend_get_branches_heights图书馆里也有dendextend

dendextend_get_branches_heights(my_dend)
#[1] 2.195193 2.661372 2.837259 2.890944 3.745600 4.098533 4.177088 5.514541 6.496675
于 2017-02-15T02:04:36.673 回答
1

要获取树状图中所有节点的高度,可以使用get_nodes_attrdendextend 包中的函数。

library(dendextend)
get_nodes_attr(my_dend, "height")

 [1] 6.496675 0.000000 5.514541 3.745600 2.195193 0.000000 0.000000 2.890944
 [9] 0.000000 0.000000 4.177088 2.837259 0.000000 0.000000 4.098533 0.000000
[17] 2.661372 0.000000 0.000000
于 2017-02-15T02:18:35.970 回答
0

This is by far from elegant but works:

save the output of

dendrapply(my_dend, function(n) utils::str(attributes(n)))

to a file, and edit that file:

  out.fn <- "dendrogram.output"
  capture.output(dendrapply(my_dend, function(n) utils::str(attributes(n))),file=out.fn)
  system(paste0("sed -i '/List of/d' ",out.fn))
  system(paste0("sed -i '/\\[\\[/d' ",out.fn))
  system(paste0("sed -i '/NULL/d' ",out.fn))
  system(paste0("sed -i '/^$/d' ",out.fn))
  system(paste0("sed -i '/class/d' ",out.fn))
  system(paste0("sed -i '/midpoint/d' ",out.fn))
  system(paste0("sed -i '/leaf/d' ",out.fn))
  system(paste0("sed -i '/label/d' ",out.fn))
  system(paste0("sed -i '/members/d' ",out.fn))
  system(paste0("sed -i 's/ \\$ //g' ",out.fn))
  system(paste0("perl -i -pe 's/height\\s+:\\s+num\\s+//g' ",out.fn))

  my_dend.df <- dplyr::filter(read.table(out.fn,header=F,sep=",",stringsAsFactors=F,col.names="depth"),depth != 0)

> my_dend.df
  depth
1  6.50
2  5.51
3  3.75
4  2.20
5  2.89
6  4.18
7  2.84
8  4.10
9  2.66
于 2017-02-18T02:35:13.357 回答