可能重复:
如何使用 R 应用分层或 k 均值聚类分析?
考虑这四个列数相同但行数不同的矩阵
library(gtools)
m1 <- matrix(sample(c(-1, 0, 1), 15, replace=T), 3)
m2 <- matrix(sample(c(-1, 0, 1), 25, replace=T), 5)
m3 <- matrix(sample(c(-1, 0, 1), 25, replace=T), 5)
m4 <- matrix(sample(c(-1, 0, 1), 30, replace=T), 6)
rownames(m1) <- c(1:3)
rownames(m2) <- c(4:8)
rownames(m3) <- c(9:13)
rownames(m4) <- c(14:19)
hclust()
当按以下格式排列时,我想应用于这四个矩阵:
mat <- list(m1, m2, m3, m4)
unite <- rbind(m1,m2,m3, m4)
rownames(unite) <- c(1:19)
distUnite <- as.matrix(dist(unite, method="manhattan"))
## empty matrix for storing the distance between pairwise matrices
dist4m <- matrix(0, nrow=4, ncol=4)
indices <- combinations(4,2)
distance <- apply(indices, 1,
function(pair){
print(pair)
s1=pair[1]
s2=pair[2]
pairmean <- mean(distReads[which(m$Sample==samples[s1]), which(m$Sample==samples[s2])])
dist4m[s1,s2] <<- pairmean
dist4m[s2,s1] <<- pairmean
})
print(dist4m)
## then use hclust(), and plot()
上面的脚本应该可以工作,但我想知道是否有更有效和可靠的方法来解决?
谢谢你的建议。