[R] maxtrix to permutation vector

Luc Villandre villandl at dms.umontreal.ca
Fri May 29 21:11:11 CEST 2009


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. 
>
>  
>
> 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.
>   

Hi Ian,

Here's one way you could do it (assuming ori.mat is the original matrix 
and ori.mat has labeled columns and rows):

> a.matrix <- expand.grid(rownames(ori.mat),colnames(ori.mat))
> final.answer <- cbind(a.matrix,ori.mat[seq_along(ori.mat)])

Cheers,

-- 
*Luc Villandré*
/Biostatistician
McGill University Health Center -
Montreal Children's Hospital Research Institute/




More information about the R-help mailing list