将树形图导出为 R 中的表格

2024-04-30

我想将 hclust-dendrogram 从 R 导出到数据表中,以便随后将其导入到另一个(“自制”)软件中。str(unclass(fit))提供了树状图的文本概述,但我正在寻找的实际上是一个数字表。我查看了 Bioconductor ctc 包,但它产生的输出看起来有些神秘。我想要类似这个表的东西:http://stn.spotfire.com/spotfire_client_help/heat/heat_importing_exporting_dendrograms.htm http://stn.spotfire.com/spotfire_client_help/heat/heat_importing_exporting_dendrograms.htm有没有办法从 R 中的 hclust 对象中获取它?


如果有人也对树状图导出感兴趣,这是我的解决方案。最有可能的是,它不是最好的,因为我最近才开始使用 R,但至少它有效。因此,欢迎提出有关如何改进代码的建议。

So, ifhr是我的 hclust 对象并且df是我的数据,第一列包含一个从0开始的简单索引,行名称是聚集项的名称:

# Retrieve the leaf order (row name and its position within the leaves)
leaf.order <- matrix(data=NA, ncol=2, nrow=nrow(df),
              dimnames=list(c(), c("row.num", "row.name")))
leaf.order[,2] <- hr$labels[hr$order]
for (i in 1:nrow(leaf.order)) {
   leaf.order[which(leaf.order[,2] %in% rownames(df[i,])),1] <- df[i,1]
}
leaf.order <- as.data.frame(leaf.order)

hr.merge <- hr$merge
n <- max(df[,1])

# Re-index all clustered leaves and nodes. First, all leaves are indexed starting from 0.
# Next, all nodes are indexed starting from max. index leave + 1.
for (i in 1:length(hr.merge)) {
  if (hr.merge[i]<0) {hr.merge[i] <- abs(hr.merge[i])-1}
  else { hr.merge[i] <- (hr.merge[i]+n) }
}
node.id <- c(0:length(hr.merge))

# Generate dendrogram matrix with node index in the first column.
dend <- matrix(data=NA, nrow=length(node.id), ncol=6,
           dimnames=list(c(0:(length(node.id)-1)),
              c("node.id", "parent.id", "pruning.level",
              "height", "leaf.order", "row.name")) )
dend[,1] <- c(0:((2*nrow(df))-2))  # Insert a leaf/node index

# Calculate parent ID for each leaf/node:
# 1) For each leaf/node index, find the corresponding row number within the merge-table.
# 2) Add the maximum leaf index to the row number as indexing the nodes starts after indexing all the leaves.
for (i in 1:(nrow(dend)-1)) {
  dend[i,2] <- row(hr.merge)[which(hr.merge %in% dend[i,1])]+n
}

# Generate table with indexing of all leaves (1st column) and inserting the corresponding row names into the 3rd column.
hr.order <- matrix(data=NA,
           nrow=length(hr$labels), ncol=3,
           dimnames=list(c(), c("order.number", "leaf.id", "row.name")))
hr.order[,1] <- c(0:(nrow(hr.order)-1))
hr.order[,3] <- t(hr$labels[hr$order])
hr.order <- data.frame(hr.order)
hr.order[,1] <- as.numeric(hr.order[,1])

# Assign the row name to each leaf.
dend <- as.data.frame(dend)
for (i in 1:nrow(df)) {
      dend[which(dend[,1] %in% df[i,1]),6] <- rownames(df[i,])
}

# Assign the position on the dendrogram (from left to right) to each leaf.
for (i in 1:nrow(hr.order)) {
      dend[which(dend[,6] %in% hr.order[i,3]),5] <- hr.order[i,1]-1
}

# Insert height for each node.
dend[c((n+2):nrow(dend)),4] <- hr$height

# All leaves get the highest possible pruning level
dend[which(dend[,1] <= n),3] <- nrow(hr.merge)

# The nodes get a decreasing index starting from the pruning level of the
# leaves minus 1 and up to 0

for (i in (n+2):nrow(dend)) {
   if ((dend[i,4] != dend[(i-1),4]) || is.na(dend[(i-1),4])){
        dend[i,3] <- dend[(i-1),3]-1}
      else { dend[i,3] <- dend[(i-1),3] }
}
dend[,3] <- dend[,3]-min(dend[,3])

dend <- dend[order(-node.id),]

# Write results table.
write.table(dend, file="path", sep=";", row.names=F)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将树形图导出为 R 中的表格 的相关文章

随机推荐

  • 主题中的文本外观

    我想将主题中的文本外观设置为 TextAppearnance Large 这是我在 styles xml 中所做的事情 我的应用程序在清单中指向这个主题 问题 我的文字仍然显示很小 问题 我在尝试在活动中使用预定义的 TextAppeara
  • 迭代时跳过相同的多重映射值

    有没有什么好方法可以实现下面所需的输出 而不必删除相同的值或创建另一个列表 向量等 我正在尝试将不同文档中找到的单词映射到其文档名称 如所需输出中所示 include
  • self.navigationController?.popViewControllerAnimated 来自 UIAlertController

    我是 swift 的新手 但我想我已经掌握了它的窍门 但这对我的进步来说是相当困难的 我想做的是 当我们找不到与用户查询相关的数据时 向用户抛出一条错误消息 然后继续将他带回之前的 ViewController 然而 我在做这件事时遇到了真
  • 如何获取 Android 中其他应用程序的屏幕时间?

    我想达到在 Android 系统上运行的每个应用程序的屏幕时间 例如 Facebook 工作时间为 3 小时 但屏幕时间为 1 2 小时 我怎么才能得到它 android app usage 使用情况统计 public final clas
  • MySQL 查询在基于特定标签组合获取行时返回不需要的行

    我在 Windows 8 PC 上运行 PHP MySQL 我有一张桌子mytable像下面这样 product tag lot 1111 101 2 1111 102 5 2222 103 6 3333 104 2 4444 101 2
  • 在 Sass mixin 中跳过可选参数

    我有这个 mixin 来处理简单的 CSS3 线性渐变 mixin linear gradient from to dir bottom dir webkit top ie filters false background color to
  • MapSingleType错误

    我通过 MVC3 创建一个项目 并首先使用 EF Code 作为数据访问层 在我的数据库中我有PackaginInfo表 在项目中我关心包类 这是我的代码 public class Package public decimal Packag
  • 在 python 中检查堆栈中的局部变量

    我编写了一个小函数 它在堆栈中查找一级并查看其中是否有变量 但是我如何将这个函数变成一个可以在堆栈中一直查找直到找到一个局部变量并购买某个特定名称的函数 import inspect def variable lookup variable
  • Knexjs 返回 mysql 时间戳、日期时间列作为 Javascript Date 对象

    我正在使用 knex 并以格式插入数据YYYY MM DD HH mm ss e g 2017 07 14 15 00 00 保存后 当提取数据时 日期时间列值将作为 JavaScript Date 对象返回 我想以以下格式返回这些对象YY
  • 覆盖骨干模型更改事件

    我认为我想做的事情很简单 我只是不知道该怎么做 当我的模型属性之一发生更改以便将一些数据传递给事件处理程序时 无论更改是值的增加还是减少 我想触发我自己的事件 基本上我希望我的处理程序在视图中执行此操作 handler function i
  • 如何将一列分成两列?

    我有以下 df 0 0 Fuerte venta 0 00 1 Infraponderar 0 00 2 Neutral 14 00 3 Sobreponderar 2 00 4 Fuerte compra 11 00 我怎样才能将列分成两
  • 如何更改Spring-WS的“SOAP-ENV”默认前缀

    我使用 Spring WS 创建了一个 Web 服务 为了保持与旧系统的兼容性 我需要将命名空间前缀从SOAP ENV to soap 我知道SOAP ENV and soap只是命名空间前缀 只要它们引用正确的命名空间 http sche
  • SQL 从一列到另一列搜索字符串

    以前可能有人问过这个问题 但我不知道如何搜索它 我想查找 Column2 中的字符串是否是 Column1 的一部分 或者根本没有在 Column1 中使用 Column1 Column2 ABCDE JKL XC XC PQ A XYZ
  • matplotlib 中的低对比度图像(对比度拉伸)问题

    当读取低对比度图像时 它会自动采用以下示例 In 1 from PIL import Image In 2 import numpy as np In 3 import matplotlib pyplot as plt In 4 img I
  • 迭代 C++ 映射中的键

    有没有办法迭代键 而不是 C 映射对 地图是关联容器 因此 迭代器是一对key val 如果您只需要键 则可以忽略该对中的值部分 for std map
  • rvm 与 sqlite3 冲突

    Users dev rvm gems ruby 1 9 2 head rails3 gems sqlite3 ruby 1 3 1 lib sqlite3 sqlite3 native bundle BUG 分段错误 ruby 1 8 7
  • 为 iPhone 应用程序设置自动构建服务器的最佳实践?

    我正在为我们的 iPhone 应用程序设置一个自动夜间构建服务器 并寻求有关哪些有效 哪些无效的建议 基本上 至少每晚运行所有单元测试并向我们的内部网站发布新的临时版本 所有开发人员都使用笔记本电脑 笔记本电脑将在夜间关闭 因此我正在考虑购
  • 如何在Android上的Xamarin.Forms中的NavigationBar左侧添加ToolbarItem?

    I need to have关闭button 在本例中 在左侧 of 导航栏 如下 我只需要弹出窗口 因此其他元素 导航不存在潜在问题 谷歌对此有一些建议 但我只看到了 iOS 示例 这并不是一个需要处理的大问题 iOS custom re
  • 如何在 GitHub 页面上添加与 index.md 共享相同主题的新页面?

    目前 我的 GitHub 帐户上有一个主页 https
  • 将树形图导出为 R 中的表格

    我想将 hclust dendrogram 从 R 导出到数据表中 以便随后将其导入到另一个 自制 软件中 str unclass fit 提供了树状图的文本概述 但我正在寻找的实际上是一个数字表 我查看了 Bioconductor ctc