我有一个名为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")))
答案 0 :(得分:3)
我会考虑以下事项:
data.frame(as.table(mydist))[lower.tri(mydist, diag = TRUE), ]
## Var1 Var2 Freq
## 1 S05-F13-P01_C S05-F13-P01_C 2251
## 2 S08-F10-P01_C S05-F13-P01_C 1923
## 3 S08-F11-P01_C S05-F13-P01_C 2085
## 4 S09-F66-P01_C S05-F13-P01_C 1954
## 5 S09-F67-P01_C S05-F13-P01_C 2105
## 7 S08-F10-P01_C S08-F10-P01_C 2246
## 8 S08-F11-P01_C S08-F10-P01_C 2094
## 9 S09-F66-P01_C S08-F10-P01_C 1955
## 10 S09-F67-P01_C S08-F10-P01_C 2127
## 13 S08-F11-P01_C S08-F11-P01_C 2521
## 14 S09-F66-P01_C S08-F11-P01_C 2110
## 15 S09-F67-P01_C S08-F11-P01_C 2329
## 19 S09-F66-P01_C S09-F66-P01_C 2276
## 20 S09-F67-P01_C S09-F66-P01_C 2141
## 25 S09-F67-P01_C S09-F67-P01_C 2561
答案 1 :(得分:2)
我们可以尝试
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])
答案 2 :(得分:1)
这似乎有效:
cbind(rownames(mydist)[which(lower.tri(mydist, diag=T), arr.ind=T)[,1]],
colnames(mydist)[which(lower.tri(mydist, diag=T), arr.ind=T)[,2]],
mydist[lower.tri(mydist, diag=T)])
或者,将其转换为@akrun的data.frame:
temp1 <-data.frame(sampleA=rownames(mydist)[which(lower.tri(mydist, diag=T), arr.ind=T)[,1]],
sampleB=colnames(mydist)[which(lower.tri(mydist, diag=T), arr.ind=T)[,2]],
value=as.numeric(mydist[lower.tri(mydist, diag=T)]), stringsAsFactors=F)