10

Let me create some data before I ask my question.

 my.data <- data.frame(A = sample(seq(1,100,by=5),10,replace=TRUE),W = rnorm(10),X =sample(1:10),Y = sample(c("yes", "no"), 10, replace = TRUE),Z=sample(c('a','b','c','d'),10,replace=TRUE))

attach(my.data)

my.d <- xtabs(W~Z+Y+A);my.d
table.data <- ftable(my.d)

result1 <- round(table.data,2)

result1 looks like ..

      A     6    11    16    26    71    76    86    91
Z Y                                                    
a no     0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
 yes    0.00  0.56  0.00  0.00  0.00  0.79  0.00  0.01

b no     0.61  0.00 -0.22  0.14  0.00  0.00 -0.08  1.71
  yes    0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00

c no     0.00  0.00  0.00  0.00 -0.08  0.00  0.00  0.00
  yes    0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00

d no     0.00  0.00  0.00  0.00  1.00  0.00  0.00  0.00
  yes    0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00

I am actually writing an article using knitr package. Is there a way to transform result1 into a latex table automatically when my *.rnw file is complied ?

I tried with xtable but got the following error...

Error in UseMethod("xtable") :   no applicable method for 'xtable' applied to an object of class "ftable"

Thank you @DWin and @Yihui. Apart from latex(), I also used xtable as stated under

print(xtable(ftable2data.frame(result1)))

To remove unnecessary row names I did the following

print(xtable(ftable2data.frame(result1)),include.rownames=FALSE)
Stat-R
  • 5,040
  • 8
  • 42
  • 68

5 Answers5

8

As an alternative, memisc provides toLatex methods for ftable objects.

library(memisc)
toLatex(result1)
David
  • 91
  • 1
  • 3
5

You can use the package xtable:

library(xtable)
mytable=ftable(mydata)
print(
  xtable(format(mytable)),file="~/Desktop/mytable.tex"
)

I don't know how it compares to the other options given.

user109114
  • 403
  • 4
  • 10
5

Method 1:

require(MIfuns)
require(Hmisc)
latex(ftable2data.frame(result1))
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • latex(ftable2data.frame(result1)) does not seem to be responding @ DWin – Stat-R Mar 11 '12 at 22:38
  • You supporting LaTeX setup may be different than mine. – IRTFM Mar 11 '12 at 22:49
  • 1
    I guess you need to prevent `latex()` from compiling the tex file automatically (by default): use `latex(ftable2data.frame(result1), file='')`; also remember to set the chunk option `results=asis` or `results=tex` – Yihui Xie Mar 12 '12 at 01:04
  • This package is no longer available. `install.packages("MIfuns") Warning in install.packages : package ‘MIfuns’ is not available (for R version 3.1.2)` (It was replaced by `memisc`, see David's answer. – s_a May 05 '15 at 14:12
2

Building off of user2030503's answer,

# install.packages('simsalapar')
library(simsalapar)
utils::toLatex(result1)

The function toLatex is an S3 generic so passes to simsalapar:::toLatex.ftable() when given an ftable object. Alternatively you can just do simsalapar:::toLatex.ftable(result1).

Once that was done I needed to include \usepackage{booktabs} in the latex preamble as toLatex.ftable uses \toprule. Alternatively you can pass booktabs=FALSE.

It also looks like toLatex.ftable trims out trailing zeroes. To fix that this is what I did (see the answer to Formatting Decimal places in R for format()):

result1[1:nrow(result1),1:ncol(result1)] %<>% as.numeric %>% format(nsmall=2,digits=3)

this converts the matrix of the ftable to a character matrix, but toLatex.ftable still works.

I also found it helpful to \usepackage{pdflscape} and wrap my table with \begin{landscape} and \end{landscape} because these contingency tables can be quite wide.

Community
  • 1
  • 1
Richard DiSalvo
  • 850
  • 12
  • 16
1

Use the toLatex() function provided by the simsalapar Package.

library(simsalapar)
toLatex(result1)
user2030503
  • 3,064
  • 2
  • 36
  • 53