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

查看:17
本文介绍了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?

提前谢谢你

推荐答案

你将不得不更具体地知道你究竟想要制表什么,但我猜 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)给你乳胶代码:

egin{tabular}{llcccc}
hline
& & multicolumn{2}{c}{colLabel1} & multicolumn{2}{c}{colLabel2} \ 
 &  & a1 & a2 & a1 & multicolumn{1}{c}{a2} \ 
hline

opagebreak rowLabel1 & 
opagebreak b1  & $-0.1450$ & $phantom{-}0.2633$ & $phantom{-}0.91454$ & $phantom{-}0.1222$ \
 & 
opagebreak b2  & $-0.1499$ & $-0.4290$ & $-0.09706$ & $-0.6977$ \

ule{0pt}{1.7
ormalbaselineskip}rowLabel2 & 
opagebreak b1  & $phantom{-}0.6976$ & $-0.4888$ & $-0.68492$ & $phantom{-}1.6764$ \
 & 
opagebreak b2  & $-0.2369$ & $-0.1428$ & $-0.66405$ & $phantom{-}0.9469$ \
hline 
end{tabular}

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

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