[英]Creating a matrix from a list of pairwise comparisons
有人很好,可以解決我的第一個問題(對矩陣集合的成對“全部”與“全部”組合使用函數):
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
有兩個問題:
我可以將比較對象的兩個名稱寫入“ $”行嗎? 用
split(Cmb1,col(Cmb1)
可以獲取名稱。
感謝您抽出寶貴的時間。
這是你想要的嗎...?
(使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.