In R, when using named rows, can a sparse matrix column (merged) be added to another sparse matrix?

I have two sparse matrices, m1and 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 .
>

and I want cbind()them to make a sparse matrix like:

  [,1] [,2] 
a    .    5
b    4    .
d    .    .

however, cbind()ignores named strings:

> cbind(m1[,1],m2[,1])
  [,1] [,2]
b    4    5
d    0    0

Is there a way to do this without a loop of brute force?

+3
source share
1 answer

, Matrix. cBind, , , , ( deparse.level 2).

+2

Source: https://habr.com/ru/post/1737755/


All Articles