RE: [R] transform large matrix into list

From: Jim Brennan <jfbrennan_at_rogers.com>
Date: Wed 08 Jun 2005 - 00:00:38 EST


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@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Stefan Mischke Sent: June 7, 2005 8:56 AM
To: r-help@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@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 Received on Wed Jun 08 00:36:31 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:26 EST