gpt4 book ai didi

r - 如何将距离矩阵的下三角形提取为R中的成对列值

转载 作者:行者123 更新时间:2023-12-02 07:23:40 24 4
gpt4 key购买 nike

我有一个名为 mydist 的距离矩阵。我想将矩阵的下三角形提取为列值的成对组合。例如:

sampleA          sampleB              values
S05-F13-P01_C S05-F13-P01_C 2251
S08-F10-P01_C S08-F10-P01_C 2246

。.以此类推

数据

mydist<-structure(c("2251", "1923", "2085", "1954", "2105", "0", "2246", 
"2094", "1955", "2127", "0", "0", "2521", "2110", "2329", "0",
"0", "0", "2276", "2141", "0", "0", "0", "0", "2561"), .Dim = c(5L,
5L), .Dimnames = list(c("S05-F13-P01_C", "S08-F10-P01_C", "S08-F11-P01_C",
"S09-F66-P01_C", "S09-F67-P01_C"), c("S05-F13-P01_C", "S08-F10-P01_C",
"S08-F11-P01_C", "S09-F66-P01_C", "S09-F67-P01_C")))

最佳答案

我们可以试试

i1 <- lower.tri(mydist, diag=TRUE)
i2 <- which(i1, arr.ind=TRUE)
data.frame(sampleA = colnames(mydist)[i2[,1]],
sampleB = colnames(mydist)[i2[,2]], value = mydist[i1])

关于r - 如何将距离矩阵的下三角形提取为R中的成对列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36937359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com