【问题标题】:Maintain individual matrix names from a list in R从 R 中的列表中维护单个矩阵名称
【发布时间】:2020-04-24 14:41:40
【问题描述】:

我在 R 中的一个列表中有一组 94 个矩阵。每个矩阵的大小不同;示例如下:

 > summary(full_matrix)
            Length Class  Mode   
 Alex_1         64 -none- numeric
 Alex_10      2500 -none- numeric
 Alex_11      2916 -none- numeric
 Alex_12     20736 -none- numeric
 Alex_13     28900 -none- numeric
 Alex_14     62500 -none- numeric
 Alex_15     93025 -none- numeric
 Alex_2        100 -none- numeric
 Alex_3         25 -none- numeric
 Alex_4       1225 -none- numeric
 Alex_5       2304 -none- numeric
 Alex_6       1849 -none- numeric

我想使用lapply() 从每个矩阵中提取数据。我正在对每个矩阵进行聚类分析,为每个矩阵生成一个聚类子集。我可以使用以下代码来做到这一点:

 library(pvclust)

 clustering_data <- lapply(full_matrix, FUN = function(element) {
   result <- pvclust(element, method.dist="cor", method.hclust="average", nboot=1000, parallel=TRUE)
   output <- pvpick(result, alpha=0.95, pv="au", type="geq", max.only=TRUE)
   })

例如,对于clustering_data[[1]],这给了我:

 > clustering_data[[1]]
 $clusters
 $clusters[[1]]
 [1] "bah"   "hello" "huh"   "ooh"   "wee"   "woo"  

 $edges
 [1] 5

问题是我需要能够识别生成集群的原始矩阵的名称(Alex_1Alex_2 等),但我不知道该怎么做。我已经使用df %&gt;% split(., f = .$var1) 为以前的lapply() 函数完成了它,但是当对象在列表中时我无法弄清楚。

【问题讨论】:

  • 请参阅stackoverflow.com/questions/9950144/…,尤其是 map/mapply 选项,以便在这里对问题进行相当长的讨论。
  • 谢谢。我将lapply() 块更改为以下clustering_data &lt;- mapply(FUN = function(list.elem) {...}, list.elem = full_matrix, names = n) 但我收到错误Error in (function (list.elem) : unused argument (names = dots[[2]][[1]])。我对列表和lapply() 完全陌生,所以我发现这很难导航!您能否提供更多关于如何构建代码的意见?
  • 请举例说明你想要的输出应该是什么样子。

标签: r list lapply


【解决方案1】:

您应该已经在clustering_datanames 中有输出。检查:

names(clustering_data)

或者

clustering_data[1]

【讨论】:

  • 这并没有完全满足我的需要,因为我不想简单地查看名称,而是要将它们附加到列表中的每个矩阵。这就是我为解决问题所做的:clustering_data_names &lt;- sapply(clustering_data, [, "clusters")
  • @CatherineLaing 但我认为你的问题是I need to be able to identify the name of the original matrix (Alex_1, Alex_2, etc) from which the cluster is generated。它没有给你原始矩阵的名称。无论如何,可能你的意思是Map(cbind, names(full_matrix), sapply(clustering_data, `[[`, "clusters"))
猜你喜欢
  • 1970-01-01
  • 2014-05-31
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多