6

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?

dlaehnemann
  • 671
  • 5
  • 17
user702432
  • 11,898
  • 21
  • 55
  • 70
  • 1
    The [tables](http://cran.r-project.org/web/packages/tables/index.html) package does this sort of formatting, I believe, but the special syntax to get there might be a barrier. – baptiste May 27 '13 at 21:35
  • 1
    there's also [a convenient function](http://www.r-statistics.com/2012/01/printing-nested-tables-in-r-bridging-between-the-reshape-and-tables-packages/) to translate from reshape to tabular. – baptiste May 27 '13 at 22:54
  • I don't see why this is off-topic. -- Have you seen [this answer](http://stackoverflow.com/a/6892758/946850)? – krlmlr May 29 '13 at 12:08
  • What is off-topic? In any case, cheers for the pointers, @baptiste. Takes a moment to get into `tables`, but then it's quite powerful! – dlaehnemann May 29 '13 at 15:39

1 Answers1

1

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

Community
  • 1
  • 1
dlaehnemann
  • 671
  • 5
  • 17
  • 1
    Nope. Entirely silent on ctab(). – user702432 May 26 '13 at 14:16
  • Well, I guess you'll have to find a way to make it work with one of the packages. E.g. have a look at the `xtable` package, it provides an interface to create latex tables from the formats printed by `methods(xtable).`To reformat your data, the `reshape2` package might help. – dlaehnemann May 26 '13 at 14:42
  • 1
    xtable can handle 2 dimensions max. – user702432 May 27 '13 at 02:48
  • Yep, I realise that now. Which is why I also upvoted your question, because it is very interesting and I couldn't come up with any solution by having a quick look into it. Currently I have no time to look into more detail, but will see if I find more time the next days! – dlaehnemann May 27 '13 at 10:59
  • Thanks in advance. One would think this would be an easy thing to do. – user702432 May 27 '13 at 12:55
  • Yep, that was my initial though when posting the answer! – dlaehnemann May 27 '13 at 20:52
  • Edited my answer to include a little workaround to make your data structure work with xtable. Hope that helps! – dlaehnemann May 27 '13 at 21:26
  • Hiya! I edited my answer again, making use of baptiste's advice to work with the `tables` package. This is quite flexible in layouting tables and can also format them for LaTeX! Does this do the trick for you? – dlaehnemann May 29 '13 at 11:40