【发布时间】: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_1、Alex_2 等),但我不知道该怎么做。我已经使用df %>% split(., f = .$var1) 为以前的lapply() 函数完成了它,但是当对象在列表中时我无法弄清楚。
【问题讨论】:
-
请参阅stackoverflow.com/questions/9950144/…,尤其是 map/mapply 选项,以便在这里对问题进行相当长的讨论。
-
谢谢。我将
lapply()块更改为以下clustering_data <- mapply(FUN = function(list.elem) {...}, list.elem = full_matrix, names = n)但我收到错误Error in (function (list.elem) : unused argument (names = dots[[2]][[1]])。我对列表和lapply()完全陌生,所以我发现这很难导航!您能否提供更多关于如何构建代码的意见? -
请举例说明你想要的输出应该是什么样子。