可以在 xtable 中创建乳胶多列吗? [英] possible to create latex multicolumns in xtable?

查看:14
本文介绍了可以在 xtable 中创建乳胶多列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am using xtable with R Markdown and knitr to produce .tex files that I call with input{}. Works great, but I have not figured out how to create multicolumns like the one shown here. Does anyone know how to to this?

So far I am using:

tbl <- xtable(data, align="l r r r r r")
colnames(tbl) <- c("Variable", 
                  "Mean", "Std Dev", 
                  "Mean", "Std Dev", 
                  "Difference")
caption(tbl) <- c("Table Title")
print(tbl, 
    include.rownames=FALSE,
    caption.placement="top",
    booktabs=TRUE,
    type="latex", 
    file="output.tex")

I'd like to have a different grouping header over each "Mean" and "Std Dev" ("Treatment" and "Control").

Alternatively, is there a better method for using R Markdown/knitr to automatically generate tables? I don't want to manually edit the tables because the report needs to generate automatically.

UPDATE: @agstudy: I'm new to latex, but I think this is the output I am looking to produce automatically with xtable (or something like xtable):

egin{tabular}{lrrrrr}
  	oprule
      & multicolumn{2}{c}{Treatment} & multicolumn{2}{c}{Control} & \
  cmidrule(lr){2-3} cmidrule(lr){4-5}
  Variable & Mean & Std Dev & Mean & Std Dev & Difference \ 
  midrule
  var1 & 1 & 2 & 3 & 4 & 5 \ 
  ottomrule
end{tabular}

UPDATE 2: @Jonathan: it took me a few reads to understand what you were suggesting. I took your recommendation, and it worked.

In the R markdown chunk I now use:

tbl <- xtable(data)
print(tbl,
    only.contents=TRUE,
    include.rownames=FALSE,
    type="latex",
    digits(tbl) <- c(0,1,1,1,1,1),
    file="output/tblout.tex")

Then in the text, I use:

egin{tabular}{lddddd}
	oprule
    & multicolumn{2}{c}{Treatment} & multicolumn{2}{c}{Control} & \
    cmidrule(lr){2-3} cmidrule(lr){4-5}
    Variable  &  multicolumn{1}{r}{Mean}  &  multicolumn{1}{r}{Std Dev}  &       multicolumn{1}{r}{Mean}  &  multicolumn{1}{r}{Std Dev}  &  multicolumn{1}{r}{Difference}  \
midrule
input{../output/tblout}
ottomrule
end{tabular}

I'll see if anyone has any other suggestions for a native xtable (or other package) solution. Otherwise, I will accept your answer. Thanks!

解决方案

Assuming the form of the table is the same across runs (i.e., only the numbers are changing), my suggestion would be to use the only.contents argument to print.xtable and code the multi-column headers in by hand. To the best of my knowledge xtable is not capable of doing multi-column cells itself.

这篇关于可以在 xtable 中创建乳胶多列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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