kolonel
kolonel

Reputation: 1432

R: Export CrossTable to Latex

Is there a way to export a CrossTable (from the gmodels package) to Latex?

So if I do :

let = sample(c("A","B"), 10 , replace=TRUE)
num = sample(1:3, 10 , replace=TRUE)
tab = CrossTable(let , num, prop.c = FALSE,prop.t = FALSE, prop.chisq=FALSE)

Is there a way to export tab to a latex table?

Using CrossTable is not a preference. I just need something that gives me the counts and the row percentages in the same cell.

Upvotes: 3

Views: 3325

Answers (1)

Aleksandr Blekh
Aleksandr Blekh

Reputation: 2492

Based on the gmodels' package documentation, function CrossTable() returns results as a list. Therefore, I don't see any problems with exporting the results to LaTeX format. You just need to convert that list into a data frame. Then you have a choice of various R packages, containing functions to convert a data frame into LaTeX format. For example, you can use df2latex() from psych package. Alternatively, you can use either latex() or latexTabular(), both from Hmisc package. The former converts a data frame into a TeX file, whereas the former converts a data frame into a LaTeX code for the corresponding object in a tabular environment (a LaTeX table).

UPDATE:

Initial attempt - doesn't work, as CrossTable()'s result is not a simple list:

library(gmodels)
library(Hmisc)

let <- sample(c("A","B"), 10, replace = TRUE)
num <- sample(1:3, 10, replace = TRUE)
tab <- CrossTable(let, num, prop.c = FALSE, prop.t = FALSE, prop.chisq = FALSE)

myList <- lapply(1:ncol(tab), function(x) as.character(unlist(tab[, x])))
myDF <- as.data.frame(cbind(myList), stringsAsFactors = FALSE)
myLatex <- latexTabular(myDF)

Further efforts

Well, it's a little trickier than I initially thought, but there are two ways, as I see it. Please see below.

The first option is to convert the CrossTable to data frame

myDF <- as.data.frame(tab)

and then manually reshape the initial data frame per your requirements (sorry, I'm not too familiar with cross-tabulation).

The second option uses Rz package (installation is a bit annoying as it wants to install Gtk, but after closing GUI, you can call functions in R session normally, as follows.

library(Rz)

let <- sample(c("A","B"), 10, replace = TRUE)
num <- sample(1:3, 10, replace = TRUE)
tab <- crossTable(let, num) # note that I use crossTable() from 'Rz' package

# Console (default) output

summary(tab)
=================================
              num                
      --------------------       
let     1      2      3    Total 
---------------------------------
A          0      2      1      3
        0.0%  66.7%  33.3%   100%
B          1      2      4      7
       14.3%  28.6%  57.1%   100%
---------------------------------
Total      1      4      5     10
       10.0%  40.0%  50.0%   100%
=================================

Chi-Square Test for Independence

Number of cases in table: 10 
Number of factors: 2 
Test for independence of all factors:
    Chisq = 1.4286, df = 2, p-value = 0.4895
    Chi-squared approximation may be incorrect
Please install vcd package to output Cramer's V.

# Now use LaTeX output

summary(tab, latex = TRUE)
\begin{table}[htbp]
    \centering
    \caption{let $\times$ num}
    \begin{tabular}{lrrrr}
    \toprule
         &                      \multicolumn{3}{c}{num}                      &                           \\
    \cline{2-4}
    let  &\multicolumn{1}{c}{1}&\multicolumn{1}{c}{2}&\multicolumn{1}{c}{3}&\multicolumn{1}{c}{Total} \\
    \midrule
    A    &             0        &             2        &             1        &               3           \\
         &        0.0\%        &       66.7\%        &       33.3\%        &          100\%           \\
    B    &             1        &             2        &             4        &               7           \\
         &       14.3\%        &       28.6\%        &       57.1\%        &          100\%           \\
    \midrule
    Total&             1        &             4        &             5        &              10           \\
         &       10.0\%        &       40.0\%        &       50.0\%        &          100\%           \\
    \bottomrule
    \end{tabular}
    \end{table}

Chi-Square Test for Independence

Number of cases in table: 10 
Number of factors: 2 
Test for independence of all factors:
    Chisq = 1.4286, df = 2, p-value = 0.4895
    Chi-squared approximation may be incorrect
Please install vcd package to output Cramer's V.

Done!

Upvotes: 6

Related Questions