[R] transform large matrix into list

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Tue Jun 7 15:29:29 CEST 2005


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
>




More information about the R-help mailing list