[R] maxtrix to permutation vector

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri May 29 21:07:29 CEST 2009


On Fri, 2009-05-29 at 11:08 -0700, Ian Coe wrote:
> Hi,
> 
>    Is there a way to  convert a matrix into a vector representing all
> permutations of values and column/row headings with native R functions?
> I did this with 2 nested for loops and it took about 25 minutes to run
> on a  ~700x700 matrix.  I'm assuming there must be a smarter way to do
> this with R's vector commands, but being new to R, I'm having trouble
> making it work. 

Here is one way:

> mat <- matrix(1:9, ncol = 3)
> colnames(mat) <- c("a","b","c")
> rownames(mat) <- c("d","e","f")
> 
> rc <- expand.grid(rownames(mat), colnames(mat))
> res <- data.frame(Row = rc$Var2, Col = rc$Var1,
+                   Perm = as.vector(mat))
> res
  Row Col Perm
1   a   d    1
2   a   e    2
3   a   f    3
4   b   d    4
5   b   e    5
6   b   f    6
7   c   d    7
8   c   e    8
9   c   f    9

And timings for something similar to your 700*700 problem:

> mat <- matrix(seq_len(700*700), ncol = 700)
> colnames(mat) <- as.character(1:700)
> rownames(mat) <- as.character(1:700)
> 
> system.time({
+ rc <- expand.grid(rownames(mat), colnames(mat))
+ res <- data.frame(Row = rc$Var2, Col = rc$Var1,
+                   Perm = as.vector(mat))
+ })
   user  system elapsed 
  0.631   0.028   0.690 
> head(res)
  Row Col Perm
1   1   1    1
2   1   2    2
3   1   3    3
4   1   4    4
5   1   5    5
6   1   6    6

HTH

G

> 
>  
> 
> Thanks,
> 
> Ian
> 
>  
> 
>      [a] [b] [c]
> 
> [d]    1    4    7
> 
> [e]    2    5    8
> 
> [f]    3    6    9
> 
>  
> 
> a d 1
> 
> a e 2
> 
> a f 3
> 
> b d 4
> 
> b e 5
> 
> b f 6
> 
> c d 7
> 
> c e 8
> 
> c f 9
> 
>  
> 
> 
> 
> 
> 
> 
> 
> Ian Coe
> 
>  
> 
> Connective Capital Management, LLC
> 
> 385 Homer Ave.
> 
> Palo Alto, CA 94301
> 
> (650) 321-4826 ext. 03     
> 
>  
> 
> CONFIDENTIALITY NOTICE: This e-mail communication (inclu...{{dropped:23}}
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090529/6b37c756/attachment-0002.bin>


More information about the R-help mailing list