Many thanks for both replying and fixing the typo!

jc

On Tue, Dec 15, 2009 at 21:20, Charles C. Berry <cberry@tajo.ucsd.edu>wrote:

> On Tue, 15 Dec 2009, Jean-Christophe Domenge wrote:
>
>  Dear R gurus,
>> I'm looking for a way to expand a matrix to a data frame as detailed
>> below:
>> given a Matrix M with attribute dimnames=list(c("a","b"),c("u","v")),
>> return
>> a data frame df.M with
>> df.M$row   df.M$col   df.M$val
>> "a"            "u"           M["a","u"]
>> "b"            "v"           M["b". "v"]
>> "a"            "u"          M["a", "u"]
>> "b"            "v"           M["b", "v"]
>>
>>
> Do you really want two copies of diag(M) only or is that a typo??
>
> If a typo, try
>
>        df.M <- as.data.frame.table( M )
>        colnames( df.M ) <- c('row', 'col', 'val' )
>
> If you want what your example shows, then add this line:
>
>        df.M <- df.M[ c( 1, 4, 1, 4 ), ]
>
> HTH,
>
> Chuck
>
>
>  expand.grid(M) almost does the job as it flattens M to a vector
>> but then the attribute dimnames gets lost.
>> I can always do merge(expand.grid(dimnames(M)), expand.grid(M), by=0)
>> but I'm wondering if there is a more concise way to do it.
>>
>> Thanks for any help!
>>
>> jc
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>>
> Charles C. Berry                            (858) 534-2098
>                                            Dept of Family/Preventive
> Medicine
> E mailto:cberry@tajo.ucsd.edu               UC San Diego
> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
>
>
>

	[[alternative HTML version deleted]]


