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

Michael Friendly friendly at yorku.ca
Sun Dec 5 00:26:59 CET 2010


Hi Rich,
I was looking for something that I could make general, as a latex.ftable
method for ftable objects.  I realize you showed how to do this for a
particular case, but I'm having a bit of trouble translating this into
a general solution for 1 or more row variables and 1 or more col variables.

This is most likely because latex() offers a bewildering collection
of arguments I don't fully understand how to apply in this case.

Here is my current sketch, with test cases below, one of which fails...

latex.ftable <- function(object, numeric.dollar=FALSE,  ...) {
   cv <- attr(object,"col.vars")
   rv <- attr(object,"row.vars")
   ncv <- length(cv)
   nrv <- length(rv)
   colheads <- cv[[1]]
   cgroup <- if (ncv>1) cv[[1]] else NULL
   rowname <- rv[[2]]
   rgroup <- if (nrv>1) rv[[1]] else NULL
   latex.default(object, colheads=colheads, rowname=rowname,
   	cgroup=cgroup, rgroup=rgroup, numeric.dollar=numeric.dollar, ...)
}

 > ft <- ftable(UCBAdmissions)
 > ft
                 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
 > str(ft)
  ftable [1:4, 1:6] 512 89 313 19 353 17 207 8 120 202 ...
  - attr(*, "row.vars")=List of 2
   ..$ Admit : chr [1:2] "Admitted" "Rejected"
   ..$ Gender: chr [1:2] "Male" "Female"
  - attr(*, "col.vars")=List of 1
   ..$ Dept: chr [1:6] "A" "B" "C" "D" ...
 >

 > latex(ft, file="")
% latex.default(object, colheads = colheads, rowname = rowname, 
cgroup = cgroup, rgroup = rgroup, numeric.dollar = numeric.dollar, 
...)
%
\begin{table}[!tbp]
  \begin{center}
  \begin{tabular}{lrrrrrr}\hline\hline
\multicolumn{1}{l}{object}&\multicolumn{1}{c}{A}&\multicolumn{1}{c}{B}&\multicolumn{1}{c}{C}&\multicolumn{1}{c}{D}&\multicolumn{1}{c}{E}&\multicolumn{1}{c}{F}\tabularnewline
\hline
{\bfseries Admitted}&&&&&&\tabularnewline
~~Male&512&353&120&138& 53& 22\tabularnewline
~~Female& 89& 17&202&131& 94& 24\tabularnewline
\hline
{\bfseries Rejected}&&&&&&\tabularnewline
~~Male&313&207&205&279&138&351\tabularnewline
~~Female& 19&  8&391&244&299&317\tabularnewline
\hline
\end{tabular}

\end{center}

 > ft1 <- ftable(UCBAdmissions, col.vars=2:3)
 > ft1
          Gender Male                     Female
          Dept      A   B   C   D   E   F      A   B   C   D   E   F
Admit
Admitted         512 353 120 138  53  22     89  17 202 131  94  24
Rejected         313 207 205 279 138 351     19   8 391 244 299 317
 >
 > latex(ft1, file="")
Error in rv[[2]] : subscript out of bounds
 >

On 12/2/2010 5:24 PM, RICHARD M. HEIBERGER wrote:
> Michael,
>
> I think this is what you are looking for.
> Rich
>
>
>
> library(Hmisc)
> tmp<- array(rnorm(60), c(3,4,5),
> list(letters[1:3],LETTERS[4:7],letters[8:12]))
> ## latex(tmp)
> ftable(tmp)
> dviname<- latex(ftable(tmp))
> ft<- ftable(tmp)
> dviname<- latex(ft,
>                   colheads=attr(ft,"col.vars")[[1]],
>                   rowname=attr(ft,"row.vars")[[2]],
>                   rgroup=attr(ft,"row.vars")[[1]],
>                   n.rgroup=c(4,4,4))
>
> 	[[alternative HTML version deleted]]
>



More information about the R-help mailing list