2014-09-22 97 views
0

人是不夠好,給我從我的第一個問題解決一個矩陣(使用功能上 成對「所有VS一切」矩陣集合的組合):創建從成對比較列表

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 

有兩個問題:

  1. 我可以將比較對象的兩個名稱寫入「$」行嗎?與

名稱可以得到

分裂(Cmb1,餵食col(Cmb1)。

  • 更好的是,是有可能以某種創建矩陣具有n行和列的n個對象並填寫統計值?
  • 感謝您抽出寶貴的時間。

    +0

    你試過'sapply'在'lapply'的地方?我們不能重現這一點。你可以做一個最小可重現的例子嗎? – 2014-09-22 15:47:26

    +0

    我調整了代碼。它只是通過壁爐架統計數據來比較彼此之間的矩陣。 – nouse 2014-09-22 16:02:37

    +0

    關於你的第一個問題,如果你把這三個對象放到一個列表中,那麼你可以用'$'操作符引用它們。 – 2014-09-22 17:07:09

    回答

    1

    這是你想要的嗎?

    (使用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 
    
    +0

    我知道我們不應該這樣做,但是你爲我節省了很多工作。驚人! – nouse 2014-09-23 09:09:20