Hi, I'd like to reshape a sparse matrix generated from the Matrix package. I can't seem to do it with the command dim(A) <- c(6,9) which works perfectly with the base package matrices, but with the sparse matrices it errors with Error in dim(A) = c(6, 9) : dim<- : invalid first argument Manipulating the Dim attribute of the sparse Matrix does not produce the desired effect. A at Dim <- c(as.integer(9),as.integer(6)) does not produce a column ordering result, which I am assuming is because the data is stored in a row (i) and column (j) format instead (class dgTMatrix) Does a function for this exist? -Scott
I wrote a function to reshape a sparse matrix of type dgTmatrix It is as follows: reshape.matrix <- function(A,r,c) { ## Reshape a matrix from the Matrix package ## of type dgTmatrix ## r_old <- A at Dim[1] c_old <- A at Dim[2] if(as.numeric(r_old*c_old) != as.numeric(r*c)) stop("Invalid dimension. There must be ",r_old*c_old," elements in the new matrix") k <- r_old*A at j + A at i i <- k %% r j <- (k-i)/r ## redo spMatrix so I don't have to add one to i and j dim <- c(as.integer(r), as.integer(c)) new("dgTMatrix", x = A at x, Dim = dim, i = as.integer(i), j = as.integer(j)) } Any suggestions on improvements of checks that I am missing? -Scott
>>>>> "Scott" == Scott Hyde <hydes at byuh.edu> >>>>> on Tue, 15 May 2007 17:03:13 -1000 (HST) writes:Scott> Hi, Scott> I'd like to reshape a sparse matrix generated from the Matrix package. I can't seem to do it with the command Scott> dim(A) <- c(6,9) Scott> which works perfectly with the base package matrices, but with the sparse matrices it errors with Scott> Error in dim(A) = c(6, 9) : dim<- : invalid first argument This *does* work in the current version of Matrix (0.99875-1), actually already in version 0.99875-0 . In the next version of Matrix, it will not only work, but also work "sparsely" internally via the new class "sparseVector" and its daughter classes, on which I've been working during the last 10 days or so... Interesting that you bring the topic up right now ... Scott> Manipulating the Dim attribute of the sparse Matrix does not produce the desired effect. A at Dim <- c(as.integer(9),as.integer(6)) does not produce a column ordering result, which I am assuming is because the data is stored in a row (i) and column (j) format instead (class dgTMatrix) You should not have manipulate slots of S4 classes in general. Some people say that you should not even access them directly. Scott> Does a function for this exist? yes, as I said above dim(.) <- .. works in the newest versions of "Matrix". Regards, Martin Maechler, ETH Zurich
Thanks for the respons, Martin. The program I wrote works as well for doing the reshaping, but it is nice to have it built in. Is there a way to tell what version of Matrix is installed, and how do we know when new releases are issued? I installed Matrix fairly recently, and thought I was up to date. -Scott ---- Original message ---->Date: Wed, 16 May 2007 09:32:44 +0200 >From: Martin Maechler <maechler at stat.math.ethz.ch> >Subject: Re: [R] Reshape a sparse matrix >To: Scott Hyde <hydes at byuh.edu> >Cc: r-help at stat.math.ethz.ch > >>>>>> "Scott" == Scott Hyde <hydes at byuh.edu> >>>>>> on Tue, 15 May 2007 17:03:13 -1000 (HST) writes: > > Scott> Hi, > > Scott> I'd like to reshape a sparse matrix generated from the Matrix package. I can't seem to do it with the command > > Scott> dim(A) <- c(6,9) > > Scott> which works perfectly with the base package matrices, but with the sparse matrices it errors with > > Scott> Error in dim(A) = c(6, 9) : dim<- : invalid first argument > >This *does* work in the current version of Matrix (0.99875-1), actually >already in version 0.99875-0 . > >In the next version of Matrix, it will not only work, but also >work "sparsely" internally via the new class "sparseVector" and >its daughter classes, on which I've been working during the last >10 days or so... >Interesting that you bring the topic up right now ... > > > Scott> Manipulating the Dim attribute of the sparse Matrix does not produce the desired effect. A at Dim <- c(as.integer(9),as.integer(6)) does not produce a column ordering result, which I am assuming is because the data is stored in a row (i) and column (j) format instead (class dgTMatrix) > >You should not have manipulate slots of S4 classes in general. >Some people say that you should not even access them directly. > > Scott> Does a function for this exist? > >yes, as I said above dim(.) <- .. works in the newest versions >of "Matrix". > >Regards, >Martin Maechler, ETH Zurich