Re: [R] transform large matrix into list

From: <james.holtman_at_convergys.com>
Date: Wed 08 Jun 2005 - 01:12:20 EST

> 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@convergys.com
+1 (513) 723-2929
                                                                                                                                           
                      Stefan Mischke                                                                                                       
                      <mischke@sozpsy.unizh        To:       r-help@stat.math.ethz.ch                                                      
                      .ch>                         cc:                                                                                     
                      Sent by:                     Subject:  [R] transform large matrix into list                                          
                      r-help-bounces@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@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 01:20:02 2005

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