R:将CrossTable导出到Latex

kol*_*nel 2 r data-visualization

有没有办法将CrossTable(从gmodels包中)导出到Latex?

所以,如果我这样做:

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)
Run Code Online (Sandbox Code Playgroud)

有没有办法将标签导出到乳胶表?

使用CrossTable不是偏好.我只需要能够在同一个单元格中获得计数和行百分比的东西.

Ale*_*ekh 5

基于gmodels'包文档,函数CrossTable()将结果作为列表返回.因此,我没有看到将结果导出为LaTeX格式的任何问题.您只需将该列表转换为数据框即可.然后,您可以选择各种R包,包含将数据帧转换为LaTeX格式的函数.例如,你可以使用df2latex()psych包.或者,您可以使用包中的任一个latex()latexTabular()两者Hmisc.前者将数据帧转换为TeX文件,而前者将数据帧转换为tabular环境中相应对象的LaTeX代码(LaTeX表).

更新:

初始尝试 - 不起作用,因为CrossTable()结果不是一个简单的列表:

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)
Run Code Online (Sandbox Code Playgroud)

进一步努力

嗯,这比我最初的想法有点棘手,但有两种方式,正如我所看到的.请看下面.

第一个选项是将CrossTable转换为数据框

myDF <- as.data.frame(tab)
Run Code Online (Sandbox Code Playgroud)

然后根据您的要求手动重塑初始数据框(抱歉,我不太熟悉交叉制表).

第二个选项使用Rz包(安装有点烦人,因为它想安装Gtk,但关闭GUI后,你可以正常调用R session中的函数,如下所示.

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.
Run Code Online (Sandbox Code Playgroud)

完成!