Search code examples
rxtable

CrossTable function in other formatting


Is there a way to transform the table output created with the Crosstable function into another format?

I tried using xtable

library(gmodels)

library(descr)

TB14.4 =CrossTable(AvaliacaoEAD,instrucao,prop.r=TRUE, prop.c=TRUE,
                   prop.t=FALSE, prop.chisq=FALSE, format="SPSS" )

install.packages("xtable")
library(xtable)

xtable(TB14.4)

The error was as follows:

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

Solution

  • The CrossTable object produced by descr package is more flexible, whether you are trying to store the CrossTable output or the content of the calculations. For example, this R Markdown document:

    ---
    title: "CrossTable Example"
    output: html_document
    ---
    <style>
    .main-container {
        max-width: 940px;
        margin-left: 0;
        margin-right: auto;
    }
    </style>
    
    ```{r example, echo=FALSE, message=FALSE}
    library(descr)
    library(pander)
    my.table <- descr::CrossTable(mtcars$cyl, mtcars$mpg, prop.r=TRUE, prop.c=TRUE, prop.t=FALSE, prop.chisq=FALSE, format="SPSS")
    class(my.table)
    pander(my.table)
    ```
    

    Produces an object of type CrossTable that is easily formatted by pander even for large numbers of columns. Note that you can choose at this point to make it easier to read by setting one or more of the prop to FALSE.

    enter image description here