我有两个稀疏矩阵,m1并且m2:
> m1 <- Matrix(data=0,nrow=2, ncol=1, sparse=TRUE, dimnames=list(c("b","d"),NULL))
> m2 <- Matrix(data=0,nrow=2, ncol=1, sparse=TRUE, dimnames=list(c("a","b"),NULL))
> m1["b",1]<- 4
> m2["a",1]<- 5
> m1
2 x 1 sparse Matrix of class "dgCMatrix"
b 4
d .
> m2
2 x 1 sparse Matrix of class "dgCMatrix"
a 5
b .
>
Run Code Online (Sandbox Code Playgroud)
我希望cbind()他们制作一个稀疏矩阵,例如:
[,1] [,2]
a . 5
b 4 .
d . .
Run Code Online (Sandbox Code Playgroud)
但是cbind()忽略命名的行:
> cbind(m1[,1],m2[,1])
[,1] [,2]
b 4 5
d 0 …Run Code Online (Sandbox Code Playgroud) 我有25个稀疏矩阵的大清单(它们确实很大-其中一个中有100M或更多元素),我需要将它们合并成一个大的稀疏矩阵。
例如:一个矩阵A可能看起来像这样(它是我的100M个元素的真实矩阵的子矩阵):
> A
5 x 4 sparse Matrix of class "dgCMatrix"
SKU
CustomerID 404 457 547 558
100002_24655 1 . . .
100003_46919 . 1 1 .
100007_46702 . . . .
100012_47709 . . . .
100013_46132 1 1 1 1
> dput(A)
new("dgCMatrix"
, i = c(0L, 4L, 1L, 4L, 1L, 4L, 4L)
, p = c(0L, 2L, 4L, 6L, 7L)
, Dim = c(5L, 4L)
, Dimnames = structure(list(CustomerID = c("100002_24655", "100003_46919",
"100007_46702", "100012_47709", "100013_46132"), SKU …Run Code Online (Sandbox Code Playgroud) 如何dgCMatrix在R中合并两个大型(大约500k列和行)稀疏矩阵的正规类稀疏矩阵(不同的列和行)?
简单的例子:我有一个完整的6x6矩阵
1 2 3 4 5 6
1 0 0 0 0 0 0
2 0 0 0 0 0 0
3 0 0 0 0 0 0
4 0 0 0 0 0 0
5 0 0 0 0 0 0
6 0 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
现在我想合并不同大小的第二个矩阵:
3 4 5 6
1 0 1 0 0
3 0 0 1 0
4 1 0 0 0
Run Code Online (Sandbox Code Playgroud)
结果应该是:
1 2 3 4 5 6
1 …Run Code Online (Sandbox Code Playgroud)