Creating a latex table from ftable object in R

前端 未结 5 516
面向向阳花
面向向阳花 2021-02-04 14:08

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(         


        
相关标签:
5条回答
  • 2021-02-04 14:32

    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.

    0 讨论(0)
  • 2021-02-04 14:36

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

    library(simsalapar)
    toLatex(result1)
    
    0 讨论(0)
  • 2021-02-04 14:39

    Method 1:

    require(MIfuns)
    require(Hmisc)
    latex(ftable2data.frame(result1))
    
    0 讨论(0)
  • As an alternative, memisc provides toLatex methods for ftable objects.

    library(memisc)
    toLatex(result1)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题