我不知道我正在尝试构建的“签名矩阵”是否在任何字段中都有适当的预先存在的名称或定义,但以下代码似乎可以在某些玩具矩阵上生成正确的结果。我很难在不引起混淆的情况下解释我到底想要做什么,但如果我提供的代码不足以推断出我想要做什么,我很乐意试一试。
当我使用我的实际数据(大小均为 300 x 20,000 个元素的两个整数矩阵)运行此代码时,它似乎正在工作,但经过数小时后仍无法完成。
我知道迭代可能是这里最大的问题,但我无法弄清楚如何删除它。
编码:
# Load required library
library(Matrix)
# Load in the test data
mut <- matrix(data=c(1,1,1,0,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,0),
nrow=5,ncol=4,
dimnames=list(c("p1","p2","p3","p4","p5"),c("GA","GB","GC","GD")))
oute <- matrix(data=c(1,1,0,1,0,1,0,0,1,1,1,1,1,0,0,1,1,0,0,1),
nrow=5,ncol=4,
dimnames=list(c("p1","p2","p3","p4","p5"),c("GQ","GW","GE","GR")))
patOutMatrix <- Matrix(data=oute,sparse=TRUE)
patMutMatrix <- Matrix(data=mut,sparse=TRUE)
transposePatMutMatrix <- t(patMutMatrix)
# Build the empty matrix (with row and col names)
sigMatrix <- Matrix(0,nrow=ncol(patMutMatrix), ncol=ncol(patOutMatrix),sparse=TRUE)
rownames(sigMatrix) <- colnames(patMutMatrix)
colnames(sigMatrix) <- colnames(patOutMatrix)
# Populate sigMatrix
for (mgene in rownames(transposePatMutMatrix))
{
a <- patOutMatrix[which(transposePatMutMatrix[mgene, ] == 1, arr.ind = T), ]
# Using an IF here to get around a problem with colSums() not working on single rows
sigMatrix[mgene,] <- if (dim(as.matrix(a))[2] == 1) {
a
} else {
colSums(patOutMatrix[which(transposePatMutMatrix[mgene, ] == 1, arr.ind = T), ])
}
}
有谁知道我如何在这里更改任何内容以使其执行得更快?