5

I have two sparse matrices, m1 and 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 to cbind() them to make a sparse matrix like:

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

however cbind() ignores the named rows:

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

is there some way to do this without a brute force loop?

smci
  • 32,567
  • 20
  • 113
  • 146
ayman
  • 1,341
  • 12
  • 22
  • 3
    It would just work if you match positions and names: m1 <- Matrix(data=0,nrow=4, ncol=1, sparse=TRUE, dimnames=list(c("a","b","c","d"),NULL)) m2 <- Matrix(data=0,nrow=4, ncol=1, sparse=TRUE, dimnames=list(c("a","b","c","d"),NULL)) m1["b",1]<- 4 m2["a",1]<- 5 cbind(m1[,1],m2[,1]) but I understand this is not an answer to your question.. – momobo Mar 20 '10 at 07:56

1 Answers1

2

You should send the question to Martin Maechler, the author of much of the Matrix package. There is a cBind function, but it does not at the moment recognize rownames, only dimensions, at least as far as I can tell (even when increasing the deparse.level argument to 2).

IRTFM
  • 258,963
  • 21
  • 364
  • 487