From friendly at yorku.ca  Fri Jan 10 14:50:05 2014
From: friendly at yorku.ca (Michael Friendly)
Date: Fri, 10 Jan 2014 08:50:05 -0500
Subject: [Rd] proposal: stats::as.matrix.ftable method
Message-ID: <52CFFA8D.8000706@yorku.ca>

Following some discussion on R-help, I'd like to propose that an 
as.matrix method be added
to those available for ftable objects.  The reason for this is that 
there are a variety of situations
where one needs to flatten a 3+ way table, but then use that for 
analysis as a matrix, not just for
pretty-printing.  There is an as.table method to turn the result back to 
a table, but an as.matrix
method is missing.

 > methods(class="ftable")
[1] as.data.frame.ftable* as.table.ftable* format.ftable*
[4] head.ftable*          print.ftable tail.ftable*

    Non-visible functions are asterisked
 >

The function below was proposed by William Dunlop:

# as.matrix method for flattened tables

# modified from Willaim Dunlop, <wdunlap at tibco.com>, R-Help, 01-09-2014
as.matrix.ftable <- function(x, sep="_", ...) {
     makeDimNames <- function(vars) {
         structure(
             list(do.call(paste, c(rev(expand.grid(rev(vars))), 
list(sep=sep)))),
             names = paste(collapse=sep, names(vars))
         )
     }
     structure(
         unclass(x),
         dimnames=c(makeDimNames(attr(x, "row.vars")),
                    makeDimNames(attr(x, "col.vars"))),
         row.vars=NULL,
         col.vars=NULL)
}

Some test cases:

 > UCB <- UCBAdmissions
 > as.matrix(ftable(Dept ~ Admit + Gender, data=UCB))
                  Dept
Admit_Gender        A   B   C   D   E   F
   Admitted_Male   512 353 120 138  53  22
   Admitted_Female  89  17 202 131  94  24
   Rejected_Male   313 207 205 279 138 351
   Rejected_Female  19   8 391 244 299 317
 > as.matrix(ftable(Dept ~ ., data=UCB))
                  Dept
Admit_Gender        A   B   C   D   E   F
   Admitted_Male   512 353 120 138  53  22
   Admitted_Female  89  17 202 131  94  24
   Rejected_Male   313 207 205 279 138 351
   Rejected_Female  19   8 391 244 299 317
 > as.matrix(ftable(Admit + Gender ~ Dept, data=UCB))
     Admit_Gender
Dept Admitted_Male Admitted_Female Rejected_Male Rejected_Female
    A           512              89           313              19
    B           353              17           207               8
    C           120             202           205             391
    D           138             131           279             244
    E            53              94           138             299
    F            22              24           351             317
 > as.matrix(ftable(Admit ~ ., data=UCB))
            Admit
Gender_Dept Admitted Rejected
    Male_A        512      313
    Male_B        353      207
    Male_C        120      205
    Male_D        138      279
    Male_E         53      138
    Male_F         22      351
    Female_A       89       19
    Female_B       17        8
    Female_C      202      391
    Female_D      131      244
    Female_E       94      299
    Female_F       24      317
 >

Related functions: The vcd package defines a more extensive suite of 
similar structable functions,
including as.matrix.structable;  however that function doesn't supply 
appropriate dimnames for
the dimensions.  Given as.matrix.ftable(), that is easy to correct:

# use as.matrix.ftable, but
# need to remove other attributes: dnames, split_vertical
library(vcd)
as.matrix.structable <- function(x, sep="_", ...) {
     structure(
         as.matrix.ftable(x, sep, ...),
         dnames = NULL,
         split_vertical = NULL
         )
}

Test:

 > as.matrix(structable(Gender ~ Admit + Dept, data=UCB))
             Gender
Admit_Dept   Male Female
   Admitted_A  512     89
   Admitted_B  353     17
   Admitted_C  120    202
   Admitted_D  138    131
   Admitted_E   53     94
   Admitted_F   22     24
   Rejected_A  313     19
   Rejected_B  207      8
   Rejected_C  205    391
   Rejected_D  279    244
   Rejected_E  138    299
   Rejected_F  351    317
 > as.matrix(structable(Gender + Admit ~ Dept, data=UCB))
     Gender_Admit
Dept Male_Admitted Male_Rejected Female_Admitted Female_Rejected
    A           512           313              89              19
    B           353           207              17               8
    C           120           205             202             391
    D           138           279             131             244
    E            53           138              94             299
    F            22           351              24             317
 >

-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA



[Rd] proposal: stats::as.matrix.ftable method
Michael Friendly