我有一个名为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/

10-09 20:58