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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Liaw, Andy
Sent: Tuesday, June 07, 2005 9:42 AM
To: 'Stefan Mischke'; r-help@stat.math.ethz.ch
Subject: RE: [R] transform large matrix into list


Here's one way to do it:

> M
   x1 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@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@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}}

______________________________________________
R-help@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

Reply via email to