Arnoldsson <Goran.Arnoldsson at stat.umu.se> wrote:
> I have an 8-dimensional contingency table (640 cells) produced by the
> table function. Which is the easiest method to transform the table to a
> data frame with 8 factors and a 9th column for the frequencies (for
> further log-linear modelling)? Perhaps there is a function in a package
> somewhere in R-space?
Here''s a function I''ve used for years, mostly in S
(it''s a simple
wrapper around expand.grid):
dfify <- function(arr, value.name = "value", dn.names =
names(dimnames(arr))) {
Version <- "$Id: dfify.sfun,v 1.1 1995/10/09 16:06:12 d3a061 Exp
$"
dn <- dimnames(arr <- as.array(arr))
if(is.null(dn))
stop("Can''t data-frame-ify an array without
dimnames")
names(dn) <- dn.names
ans <- cbind(expand.grid(dn), as.vector(arr))
names(ans)[ncol(ans)] <- value.name
ans
}
The name is short for "data-frame-i-fy".
For your example, assuming your multi-way array has proper
dimnames, you''d just do:
my.data.frame <- dfify(my.array, value.name="frequency")
--Todd
--
Z. Todd Taylor
Pacific Northwest National Laboratory
Todd.Taylor at pnl.gov
Why is it unable and inability, not inable and unability?
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._