[R] Creating a Sparse Matrix from a Sparse Vector

BDGrantham at Hormel.com BDGrantham at Hormel.com
Tue Jan 20 18:53:55 CET 2009


Hello,

I am working with a sparse matrix that is approx. 13,900 by 14,100.  My
goal is to select a row out of the matrix and create a new matrix with that
row repeated 13,900 times without having to do any looping.  Example:

Starting Matrix:

exampleMatrix
3 x 4 sparse Matrix of class "dgCMatrix"

[1,] 1 . . .
[2,] . 1 . 0.5
[3,] . . 1 ..

New Matrix:..

 newExampleMatrix
3 x 4 sparse Matrix of class "dgCMatrix"

[1,] . 1 . 0.5
[2,] . 1 . 0.5
[3,] . 1 . 0.5

When I try the following I get a memory allocation error due to the size of
the array or vector:

 newExampleMatrix<-Matrix(rep(exampleMatrix[2,],times=nrow
(exampleMatrix)),nrow=nrow(exampleMatrix),ncol=ncol
(exampleMatrix),byrow=TRUE,sparse=TRUE)
 newExampleMatrix<-Matrix(exampleMatrix[2,],nrow=nrow
(exampleMatrix),ncol=ncol(exampleMatrix),byrow=TRUE,sparse=TRUE)

When I tried the next set, I got the error "Error in Matrix(as(rep
(exampleMatrix[2, ], times = nrow(exampleMatrix)),  :   invalid type/length
(S4/12) in vector allocation":

newExampleMatrix<-Matrix(as(rep(exampleMatrix[2,],times=nrow
(exampleMatrix)),"sparseVector"),nrow=nrow(exampleMatrix),ncol=ncol
(exampleMatrix),byrow=TRUE,sparse=TRUE)
newExampleMatrix<-Matrix(as(exampleMatrix[2,],"sparseVector"),nrow=nrow
(exampleMatrix),ncol=ncol(exampleMatrix),byrow=TRUE,sparse=TRUE)

And finally, when I tried the next instruction, I got the error "Error in
as.vector(x, mode) :  cannot coerce type 'S4' to vector of type 'any' Error
in as.matrix.csc(as.matrix.csr(x)) : error in evaluating the argument 'x'
in selecting a method for function 'as.matrix.csc'" :

as.matrix.csc(as(rep(currentMatrix[pivitRow,],times=nrow
(currentMatrix)),"sparseVector"),nrow=nrow(currentMatrix),ncol=ncol
(currentMatrix))

Are there any other ways to accomplish this?],

Thanks,

Brian




Notice:
This communication is an electronic communication within the meaning
of the Electronic Communications Privacy Act, 18 U.S.C. sec. 2510. Its
disclosure is strictly limited to the recipient(s) intended by the
sender of this message.  This transmission and any attachments may
contain proprietary, confidential, attorney-client privileged
information and/or attorney work product. If you are not the intended
recipient, any disclosure, copying, distribution, reliance on, or use
of any of the information contained herein is STRICTLY PROHIBITED.
Please destroy the original transmission and its attachments without
reading or saving in any matter and confirm by return email.


More information about the R-help mailing list