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"]

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.

Reply via email to