Creating a latex table from ftable object in R

前端 未结 5 526
面向向阳花
面向向阳花 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.

提交回复
热议问题