简体   繁体   English

为多个对象的成对“全部与全部”比较创建循环

[英]Creating a loop for a pairwise “all vs all” comparison of multiple objects

I would like to do a pairwise "all vs. all" combination of multiple data frames, in my case with the Mantel test. 我想对多个数据帧进行成对的“全部与全部”组合,以我的Mantel测试为例。

I have about 50 different matrices I want to compare: Obj1, Obj2, Obj3, ..., Objn 我要比较大约50种不同的矩阵:Obj1,Obj2,Obj3,...,Objn

library(vegan)
mantel(Obj1, Obj2)

Can I loop through this for all combinations, possibly omitting the redundant pairs? 我可以遍历所有组合吗,可能会省略冗余对吗? So, only doing one half of the n * (n-1)/2 ? 因此,仅做n * (n-1)/2一半?

I am collecting the results into a dataframe: 我正在将结果收集到一个数据框中:

result <- data.frame(mantel_r = NA, significance = NA)
mant <- mantel(Obj1, Obj2)
result[01, c("mantel_r", "significance")] <- mant[3:4]

Is it possible to combine the loop with this basic code? 是否可以将循环与此基本代码结合在一起? Possibly with giving names to the rows of the result.dataframe according to the input? 可能根据输入为result.dataframe的行命名?

I am guessing the mantel is from vegan 我想mantelvegan

library(vegan)   
names1 <- ls(pattern="Obj")
names1 #I created 3 matrices with names starting with Obj
#[1] "Obj1" "Obj2" "Obj3"

Cmb1 <- combn(names1, 2)
lst1 <- lapply(split(Cmb1, col(Cmb1)), function(x) unlist(mantel(get(x[1]), get(x[2]))[3:4]))

Update 更新资料

I am assuming that you want the list elements to have some names. 我假设您希望list元素具有一些名称。

names(lst1) <- sapply(lst1, function(x) { paste(paste(names(x), x, sep="_"), collapse="_") })

lst1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM