有人很好地解决了我的第一个问题(在一个矩阵集合的"all vs all“组合上使用了一个函数):
library(vegan)
#by Akrun
A <- matrix(sample.int(100, size = 50*50, replace = TRUE), nrow = 50, ncol = 50)
B <- matrix(sample.int(100, size = 50*50, replace = TRUE), nrow = 50, ncol = 50)
C <- matrix(sample.int(100, size = 50*50, replace = TRUE), nrow = 50, ncol = 50)
Obj1 <- vegdist(decostand(A,"standardize",MARGIN=2), method="euclidean")
Obj2 <- vegdist(decostand(B,"standardize",MARGIN=2), method="euclidean")
Obj3 <- vegdist(decostand(C,"standardize",MARGIN=2), method="euclidean")
names1 <- ls(pattern="Obj")
Cmb1 <- combn(names1, 2)
lapply(split(Cmb1, col(Cmb1)), function(x) unlist(mantel(get(x[1]), get(x[2]))[3:4]))这将产生一个结果列表,例如:
$`1`
statistic signif
0.03006202 0.4070000有两个问题:
名字是可以得到的。
谢谢你慢慢来。
发布于 2014-09-22 17:28:14
这就是你想要的吗?
(使用tmp
tmp <- sapply(split(Cmb1, col(Cmb1)),
function(x) unlist(mantel(get(x[1]), get(x[2]))[3:4]))注意到这是一个sapply()调用,因此更容易提取statistic数据。)
## zero matrix to fill in - change 0 to be what you want on diagonal
mstat <- matrix(0, ncol = 3, nrow = 3)
## directly fill lower triangle of matrix
mstat[lower.tri(mstat)] <- tmp[1, , drop = TRUE]
## need to transpose
tmstat <- t(mstat)
## then fill in lower triangle again, to get correct order
tmstat[lower.tri(tmstat)] <- tmp[1, , drop = TRUE]
## transpose back
mstat <- t(tmstat)
## add on identifiers
colnames(mstat) <- rownames(mstat) <- names1
> mstat
Obj1 Obj2 Obj3
Obj1 0.00000000 -0.04570113 0.03407708
Obj2 -0.04570113 0.00000000 0.04781475
Obj3 0.03407708 0.04781475 0.00000000https://stackoverflow.com/questions/25978069
复制相似问题