有人很乐意从我的第一个问题(使用函数)给我一个解决方案 成对"所有对所有"一组矩阵的组合):
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
有两个问题:
我可以将比较对象的两个名称写入&#34; $&#34;?随着
split(Cmb1,col(Cmb1)
可以获得名称。
感谢您抽出宝贵时间。
答案 0 :(得分:2)
这就是你想要的......?
(使用
中的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.00000000