Dear List I need to transform a large matrix M with many NAs into a list L with one row for each non missing cell. Every row should contain the cell value in the first column, and its coordinates of the matrix in column 2 and 3. M: x1 x2 y1 1 2 y2 4 5 y3 7 8 L: v x y 1 1 1 4 1 2 7 1 2 2 2 1 5 2 2 8 2 3 I'm trying to do this with a loop, but since my matrix is quite large (around 10k^2) this just takes a very long time. There must be a more efficient and elegant way to do this. Any hints? Thanks, Stefan
Ok, ok, I got it! Please don't laugh! My question is the example from the posting guide! Which even includes the answer. How embarrassing... Stefan Am 07.06.2005 um 14:55 schrieb Stefan Mischke:> Dear List > > I need to transform a large matrix M with many NAs into a list L with > one row for each non missing cell. Every row should contain the cell > value in the first column, and its coordinates of the matrix in column > 2 and 3. > > M: > x1 x2 > y1 1 2 > y2 4 5 > y3 7 8 > > L: > v x y > 1 1 1 > 4 1 2 > 7 1 2 > 2 2 1 > 5 2 2 > 8 2 3 > > I'm trying to do this with a loop, but since my matrix is quite large > (around 10k^2) this just takes a very long time. > There must be a more efficient and elegant way to do this. Any hints? > > Thanks, > Stefan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
one approach could be the following (maybe there is something better for large matrices): M <- cbind(c(1, 4, 7, NA, 3, 2), c(2, 5, 8, 4, NA, 3)) ############## d <- dim(M) L <- cbind(c(M), rep(1:d[2], each = d[1]), rep(1:d[1], d[2])) L[complete.cases(L), ] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Stefan Mischke" <mischke at sozpsy.unizh.ch> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, June 07, 2005 2:55 PM Subject: [R] transform large matrix into list> Dear List > > I need to transform a large matrix M with many NAs into a list L > with one row for each non missing cell. Every row should contain the > cell value in the first column, and its coordinates of the matrix in > column 2 and 3. > > M: > x1 x2 > y1 1 2 > y2 4 5 > y3 7 8 > > L: > v x y > 1 1 1 > 4 1 2 > 7 1 2 > 2 2 1 > 5 2 2 > 8 2 3 > > I'm trying to do this with a loop, but since my matrix is quite > large (around 10k^2) this just takes a very long time. > There must be a more efficient and elegant way to do this. Any > hints? > > Thanks, > Stefan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Here's one way to do it:> Mx1 x2 y1 1 2 y2 4 5 y3 7 8> (L <- cbind(v=c(M), x=c(col(M)), y=c(row(M))))v x y [1,] 1 1 1 [2,] 4 1 2 [3,] 7 1 3 [4,] 2 2 1 [5,] 5 2 2 [6,] 8 2 3 (The seemingly extraneous c()'s are to drop the dimensions of those matrices.) Are you sure your L[3, 3] is correct? Also, for a 10K^2 M, L is going to take up over 1.1GB of memory! You may want to look at SparseM and Matrix packages, which have facilities for handling sparse matrices. Andy> From: Stefan Mischke > > Dear List > > I need to transform a large matrix M with many NAs into a list L with > one row for each non missing cell. Every row should contain the cell > value in the first column, and its coordinates of the matrix > in column > 2 and 3. > > M: > x1 x2 > y1 1 2 > y2 4 5 > y3 7 8 > > L: > v x y > 1 1 1 > 4 1 2 > 7 1 2 > 2 2 1 > 5 2 2 > 8 2 3 > > I'm trying to do this with a loop, but since my matrix is quite large > (around 10k^2) this just takes a very long time. > There must be a more efficient and elegant way to do this. Any hints? > > Thanks, > Stefan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
First it looks like in L you need a 3 in row 3 col 2. Anyway I think this is a non elegant way to do it without a loop that you can easily improve I imagine and should be faster than the loop. You will have to adjust the numbers to fit you matrix size etc. R>mat<-matrix(sample(0:9,100,replace=T),50,2)#your M R>new<-matrix(mat,100,1) R>new2<-c(rep(1,50),rep(2,50)) R>new3<-c(rep(1:50,2)) R>result<-cbind(new,new2,new3)# your L Jim -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Stefan Mischke Sent: June 7, 2005 8:56 AM To: r-help at stat.math.ethz.ch Subject: [R] transform large matrix into list Dear List I need to transform a large matrix M with many NAs into a list L with one row for each non missing cell. Every row should contain the cell value in the first column, and its coordinates of the matrix in column 2 and 3. M: x1 x2 y1 1 2 y2 4 5 y3 7 8 L: v x y 1 1 1 4 1 2 7 1 2 2 2 1 5 2 2 8 2 3 I'm trying to do this with a loop, but since my matrix is quite large (around 10k^2) this just takes a very long time. There must be a more efficient and elegant way to do this. Any hints? Thanks, Stefan ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> x.1[,1] [,2] [1,] 1 4 [2,] 2 5 [3,] NA 6> cbind(x.1[!is.na(x.1)], which(!is.na(x.1), arr.ind=TRUE))row col [1,] 1 1 1 [2,] 2 2 1 [3,] 4 1 2 [4,] 5 2 2 [5,] 6 3 2>Jim __________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Convergys Labs james.holtman at convergys.com +1 (513) 723-2929 Stefan Mischke <mischke at sozpsy.unizh To: r-help at stat.math.ethz.ch .ch> cc: Sent by: Subject: [R] transform large matrix into list r-help-bounces at stat.m ath.ethz.ch 06/07/2005 08:55 Dear List I need to transform a large matrix M with many NAs into a list L with one row for each non missing cell. Every row should contain the cell value in the first column, and its coordinates of the matrix in column 2 and 3. M: x1 x2 y1 1 2 y2 4 5 y3 7 8 L: v x y 1 1 1 4 1 2 7 1 2 2 2 1 5 2 2 8 2 3 I'm trying to do this with a loop, but since my matrix is quite large (around 10k^2) this just takes a very long time. There must be a more efficient and elegant way to do this. Any hints? Thanks, Stefan ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
This is almost the "triplet representation" of sparse matrices, except the triplet representation excludes zero entries. If you have a lot of zeros, it would be worth a look at SparseM and Matrix. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Liaw, Andy Sent: Tuesday, June 07, 2005 9:42 AM To: 'Stefan Mischke'; r-help at stat.math.ethz.ch Subject: RE: [R] transform large matrix into list Here's one way to do it:> Mx1 x2 y1 1 2 y2 4 5 y3 7 8> (L <- cbind(v=c(M), x=c(col(M)), y=c(row(M))))v x y [1,] 1 1 1 [2,] 4 1 2 [3,] 7 1 3 [4,] 2 2 1 [5,] 5 2 2 [6,] 8 2 3 (The seemingly extraneous c()'s are to drop the dimensions of those matrices.) Are you sure your L[3, 3] is correct? Also, for a 10K^2 M, L is going to take up over 1.1GB of memory! You may want to look at SparseM and Matrix packages, which have facilities for handling sparse matrices. Andy> From: Stefan Mischke > > Dear List > > I need to transform a large matrix M with many NAs into a list L with > one row for each non missing cell. Every row should contain the cell > value in the first column, and its coordinates of the matrix > in column > 2 and 3. > > M: > x1 x2 > y1 1 2 > y2 4 5 > y3 7 8 > > L: > v x y > 1 1 1 > 4 1 2 > 7 1 2 > 2 2 1 > 5 2 2 > 8 2 3 > > I'm trying to do this with a loop, but since my matrix is quite large > (around 10k^2) this just takes a very long time. > There must be a more efficient and elegant way to do this. Any hints? > > Thanks, > Stefan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html ---------------------------------------------------------------------------- -- Notice: This e-mail message, together with any attachments,...{{dropped}}