Oh, I should have mentioned that the result of melt is a data.frame, not a
matrix.  You can convert with as.matrix if you like.

I should also have shown that dimnames are carried along:

> m <- matrix(1:4,2,2,dimnames=list(x=c('a','b'),y=c('x','y')))

> m
   y
x   x y
  a 1 3
  b 2 4

> melt(m)
  x y value
1 a x     1
2 b x     2
3 a y     3
4 b y     4

> as.matrix(melt(m))
     x   y   value
[1,] "a" "x" "1"
[2,] "b" "x" "2"
[3,] "a" "y" "3"
[4,] "b" "y" "4"



On Fri, May 29, 2009 at 2:53 PM, Stavros Macrakis <macrakis@alum.mit.edu>wrote:

> Not sure what you mean by "permutations" here.  I think what you mean is
> that given a matrix m, you want a matrix whose rows are c(i,j,m[i,j]) for
> all i and j.  You can use the `melt` function in the `reshape` package for
> this.  See below.
>
> Hope this helps,
>
>              -s
>
> > library(reshape)
> > melt(matrix(1:4,2,2))
>   X1 X2 value
> 1  1  1     1
> 2  2  1     2
> 3  1  2     3
> 4  2  2     4
>
> big <- matrix(1:700^2,700,700)
> > head(melt(big))
>   X1 X2 value
> 1  1  1     1
> 2  2  1     2
> 3  3  1     3
> 4  4  1     4
> 5  5  1     5
> 6  6  1     6
> > system.time(melt(big))
>    user  system elapsed
>    0.08    0.00    0.08
>
>
> On Fri, May 29, 2009 at 2:08 PM, Ian Coe <ICoe@connectcap.com> 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@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.
>>
>
>

	[[alternative HTML version deleted]]


