Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a multidimensional ctab()-table to LaTeX format

Tags:

r

latex

I am using ctab() from the catspec package to generate a 3-way table. But is there any way to format the output latex-ready? This would seem like a very simple thing to do:

library(catspec)
ctab(Titanic)

                   Survived  No Yes
Class Sex    Age                   
1st   Male   Child            0   5
             Adult          118  57
      Female Child            0   1
             Adult            4 140
2nd   Male   Child            0  11
             Adult          154  14
      Female Child            0  13
             Adult           13  80
3rd   Male   Child           35  13
             Adult          387  75
      Female Child           17  14
             Adult           89  76
Crew  Male   Child            0   0
             Adult          670 192
      Female Child            0   0
             Adult            3  20

But then what?

like image 747
user702432 Avatar asked May 26 '13 13:05

user702432


1 Answers

Thanks to baptiste for the pointer to the tables package. Have a look at the very detailed tables vignette with lots of nice examples and a systematic explanation of terms. And here is how this will make your example work (without using ctab(), though):

df <- as.data.frame.table(Titanic)

require('tables')
latex(tabular(RowFactor(Class, spacing=1) * RowFactor(Sex, spacing=1) * Factor(Age) ~ Freq * Heading() * identity * Survived, data = df ))

It is true that getting your head around how these formulas work takes a moment (and some trial and error...), but the examples in the vignette help a lot and the package is a damn flexible tool!

More info on generating latex tables in R is provided here: Tools for making latex tables in R

like image 66
dlaehnemann Avatar answered Sep 22 '22 03:09

dlaehnemann