R包xtable,如何从R创建具有多个行和列的乳胶表 [英] R package xtable, how to create a latextable with multiple rows and columns from R

查看:159
本文介绍了R包xtable,如何从R创建具有多个行和列的乳胶表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用R创建具有多行/多列功能的乳​​胶表. 我想要的乳胶表应如下所示:

My goal is to create latextable with multirow/multicolumn features from R. The latextable I want should look like this:

               colLabel  |  colLabel2
                         |
                 a1  a2  |   a3    a4  
 -------------------------------------
             b1  1    2  |   5     6
 rowLabel1               |
             b2  3    4  |   7     8
 --------------------------------------
             b3  9   10  |   13    14
 rowLabel2               |
             b4  11  12  |   15    16
 --------------------------------------

我当前正在使用xtable,但无法弄清楚如何使用此包创建多行. 谁能告诉我如何创建这样的表?

I am currently using xtable but I cannot figure out how to create multirow using this package. Could anyone tell me how to create such table?

提前谢谢

推荐答案

您将必须更确切地说明要尝试制表的内容,但是我想tables包中的tabular函数可能会有所帮助.

You will have to be more specific about what exactly you are trying to tabulate, but I guess tabular function from the tables package might be helpful.

下面是根据4个二元因子变量对变量进行制表的示例:

Here's an example of tabulating means of a variable according to 4 binary factor variables:

mydf <- data.frame(rowFactor1 = sample(letters[1:2], 100, replace = TRUE), 
                colFactor1 = sample(LETTERS[1:2], 100, replace = TRUE), 
                x = rnorm(100), 
                rowFactor2 = sample(1:2, 100, replace = TRUE), 
                colFactor2 = sample(1:2, 100, replace = TRUE))

tab1 <- tabular(Heading()*RowFactor(rowFactor2, spacing = 1, 
                        levelnames = c("rowLabel1", "rowLabel2"))*
                Heading()*RowFactor(rowFactor1, 
                        levelnames = c("b1", "b2")) ~ 
                Heading()*Factor(colFactor2, 
                        levelnames = c("colLabel1", "colLabel2") )*
                Heading()*Factor(colFactor1, 
                        levelnames = c("a1", "a2"))*
                Heading()*(x)*Heading()*(mean), 
        data = mydf)

可以给你类似的东西,但是使用乳胶输出时可以很好地格式化

which gives you something like this, but nicely formated when using the latex output

                                                                colLabel1         colLabel2        
                                                                a1        a2      a1        a2     
 \\nopagebreak rowLabel1                       \\nopagebreak b1 -0.1450    0.2633  0.91454   0.1222
                                               \\nopagebreak b2 -0.1499   -0.4290 -0.09706  -0.6977
 \\rule{0pt}{1.7\\normalbaselineskip}rowLabel2 \\nopagebreak b1  0.6976   -0.4888 -0.68492   1.6764
                                               \\nopagebreak b2 -0.2369   -0.1428 -0.66405   0.9469

最后latex(tab1)为您提供乳胶代码:

Finally latex(tab1) gives you the latex code:

\begin{tabular}{llcccc}
\hline
& & \multicolumn{2}{c}{colLabel1} & \multicolumn{2}{c}{colLabel2} \\ 
 &  & a1 & a2 & a1 & \multicolumn{1}{c}{a2} \\ 
\hline
\nopagebreak rowLabel1 & \nopagebreak b1  & $-0.1450$ & $\phantom{-}0.2633$ & $\phantom{-}0.91454$ & $\phantom{-}0.1222$ \\
 & \nopagebreak b2  & $-0.1499$ & $-0.4290$ & $-0.09706$ & $-0.6977$ \\
\rule{0pt}{1.7\normalbaselineskip}rowLabel2 & \nopagebreak b1  & $\phantom{-}0.6976$ & $-0.4888$ & $-0.68492$ & $\phantom{-}1.6764$ \\
 & \nopagebreak b2  & $-0.2369$ & $-0.1428$ & $-0.66405$ & $\phantom{-}0.9469$ \\
\hline 
\end{tabular}

这篇关于R包xtable,如何从R创建具有多个行和列的乳胶表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆