rfo*_*ley 1 r matrix sparse-matrix triangular bigdata
我在R中有一个非常大的(大约9100万个非零条目)sparseMatrix()看起来像:
> myMatrix
a b c
a . 1 2
b 1 . .
c 2 . .
Run Code Online (Sandbox Code Playgroud)
我想将它转换为三角形矩阵(上部或下部),但是当我尝试myMatrix = myMatrix*lower.tri(myMatrix)时,会出现一个错误,即lower.tri()的"问题太大".想知道是否有人可能知道解决方案.谢谢你的帮助!
而不是在矩阵本身上工作,而是工作summary
:
library(Matrix)
myMatrix <- sparseMatrix(
i = c(1,1,2,3),
j = c(2,3,1,1),
x = c(1,2,1,2))
myMatrix
# 3 x 3 sparse Matrix of class "dgCMatrix"
#
# [1,] . 1 2
# [2,] 1 . .
# [3,] 2 . .
mat.summ <- summary(myMatrix)
lower.summ <- subset(mat.summ, i >= j)
sparseMatrix(i = lower.summ$i,
j = lower.summ$j,
x = lower.summ$x,
dims = dim(myMatrix))
# 3 x 3 sparse Matrix of class "dgCMatrix"
#
# [1,] . . .
# [2,] 1 . .
# [3,] 2 . .
Run Code Online (Sandbox Code Playgroud)