[R] latex tables for 3+ dimensional tables/arrays

Michael Friendly friendly at yorku.ca
Thu Dec 2 21:47:23 CET 2010


I'm looking for an R method to produce latex versions of tables for 
table/array objects of 3 or more dimensions,
which, of necessity is flattened to a 2D display, for example with 
ftable(), or vcd::structable, as shown below.
I'd be happy to settle for a flexible solution for the 3D case.

 > UCB <- aperm(UCBAdmissions, c(2, 1, 3))
 > ftable(UCB)
                 Dept   A   B   C   D   E   F
Gender Admit
Male   Admitted      512 353 120 138  53  22
        Rejected      313 207 205 279 138 351
Female Admitted       89  17 202 131  94  24
        Rejected       19   8 391 244 299 317
 > structable(Dept ~ Admit+Gender, UCBAdmissions)
                 Dept   A   B   C   D   E   F
Admit    Gender
Admitted Male        512 353 120 138  53  22
          Female       89  17 202 131  94  24
Rejected Male        313 207 205 279 138 351
          Female       19   8 391 244 299 317


I tried using the xtable package but discovered that xtable.table is 
silently
wrong for tables of length(dim(x))>2.  E.g., for a 2 x 2 x 6 table, we 
get only UCBAdmissions[,,1]

 > xtable(UCBAdmissions)
% latex table generated in R 2.11.1 by xtable 1.5-6 package
% Tue Nov 16 13:23:44 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
   \hline
& Male & Female \\
   \hline
Admitted & 512.00 & 89.00 \\
   Rejected & 313.00 & 19.00 \\
    \hline
\end{tabular}
\end{center}
\end{table}
 >

The author of xtable tells me that only 2-way tables are presently 
implemented, and, while I would normally
look to Hmisc for all things LaTeX, I don't find anything useful there:

 > library(Hmisc)
 > methods("latex")
  [1] latex.bystats                  latex.bystats2
  [3] latex.default                  latex.describe
  [5] latex.describe.single          latex.function
  [7] latex.list                     latex.responseSummary
  [9] latex.summary.formula.cross    latex.summary.formula.response
[11] latex.summary.formula.reverse
 > latex(UCB)
Error in x[, j] : incorrect number of dimensions
 > traceback()
3: format.df(object, dcolumn = dcolumn, na.blank = na.blank, 
numeric.dollar = numeric.dollar,
        cdot = cdot, math.row.names = math.row.names, math.col.names = 
math.col.names,
        double.slash = double.slash, ...)
2: latex.default(UCB)
1: latex(UCB)


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



More information about the R-help mailing list