当我处理大量数据集时,我收到的数据已经很少了。我以常规方式加载它,我的矩阵'smskin'属于类:
Formal class 'dsCMatrix' [package "Matrix"] with 7 slots
..@ i : int [1:57263] 0 1 2 3 4 5 6 7 8 9 ...
..@ p : int [1:26018] 0 1 2 3 4 5 6 7 8 9 ...
..@ Dim : int [1:2] 26017 26017
..@ Dimnames:List of 2
.. ..$ : NULL
.. ..$ : NULL
..@ x : num [1:57263] 0 0 0 0 0 0 0 0 0 0 ...
..@ uplo : chr "U"
..@ factors : list()
我的矩阵是26000x26000,所有对角线值都是1,虽然我想摆脱它们以节省一些额外的空间。我这样做 diag(smskin)= 0 现在,为了节省更多空间,我想将这些零值转换为。稀疏的位置。 所以我这样做 new.smskin = Matrix(smskin,sparse = TRUE) 但是,当我查看对角线的输出时,结果是
10 x 10 sparse Matrix of class "dgCMatrix"
[1,] 0 . . . . . . . . .
[2,] . 0 . . . . . . . .
[3,] . . 0 . . . . . . .
[4,] . . . 0 . . . . . .
[5,] . . . . 0 . . . . .
[6,] . . . . . 0 . . . .
[7,] . . . . . . 0 . . .
[8,] . . . . . . . 0 . .
[9,] . . . . . . . . 0 .
[10,] . . . . . . . . . 0
看起来0值不被接受为稀疏值。 我不确定我需要做什么,但我真的需要这个对角线也是稀疏的。
最佳, ž
答案 0 :(得分:1)
有点像黑客攻击,但如果你将它乘以自己的逻辑掩码,它将解决问题:
#Setting up smskin
smskin<-rsparsematrix(nrow=26000,ncol=26000,density=0)
diag(smskin)<-1
str(smskin)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..@ i : int [1:26000] 0 1 2 3 4 5 6 7 8 9 ...
..@ p : int [1:26001] 0 1 2 3 4 5 6 7 8 9 ...
..@ Dim : int [1:2] 26000 26000
..@ Dimnames:List of 2
.. ..$ : NULL
.. ..$ : NULL
..@ x : num [1:26000] 1 1 1 1 1 1 1 1 1 1 ...
..@ factors : list()
#
diag(smskin)<-0
str(smskin)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..@ i : int [1:26000] 0 1 2 3 4 5 6 7 8 9 ...
..@ p : int [1:26001] 0 1 2 3 4 5 6 7 8 9 ...
..@ Dim : int [1:2] 26000 26000
..@ Dimnames:List of 2
.. ..$ : NULL
.. ..$ : NULL
..@ x : num [1:26000] 0 0 0 0 0 0 0 0 0 0 ...
..@ factors : list()
smskin<-((smskin!=0)*smskin)
str(smskin)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..@ i : int(0)
..@ p : int [1:26001] 0 0 0 0 0 0 0 0 0 0 ...
..@ Dim : int [1:2] 26000 26000
..@ Dimnames:List of 2
.. ..$ : NULL
.. ..$ : NULL
..@ x : num(0)
..@ factors : list()
答案 1 :(得分:1)
目前尚不清楚为何有时会发生这种情况。但是,Matrix
包中提供了一个功能来修复它:drop0
。
smskin<-drop0(smskin)