带多列和书签的R xtable [英] R xtable with multicolumns and booktabs
问题描述
我想将data.frame转换为具有多列的(booktab)乳胶表,但是我无法创建将 \ toprule
放在表顶部的输出.使用了以下数据
dat<-结构(c(841.8,804.4,135.1,106.2,0.7025,0.09645,305.2,707.1、449.3、119.9、0.7025、0.09645)、. Dim = c(2L,6L)、. Dimnames = list(c("ev","smooth"),c("Mean","SD","best","Mean","SD",最好的")))>达特平均SD最佳平均SD最佳ev 841.8 135.1 0.70250 305.2 449.3 0.70250平滑804.4 106.2 0.09645 707.1 119.9 0.09645addtorow<-list()addtorow $ pos<-list(-1)addtorow $ command<-'&\\ multicolumn {3} {c} {Tab a}&\\ multicolumn {3} {c} {Tab b} \\\\'打印(xtable(dat),add.to.row = addtorow,include.colnames = TRUE,booktabs = TRUE)
输出看起来几乎正确,但是 \ toprule
的位置错误.
\ begin {table} [ht]\定心\ begin {tabular} {rrrrrrr}&\ multicolumn {3} {c} {样本中}&\ multicolumn {3} {c} {样本外} \\\ toprule&均值标清最好的均值标清最好的 \\\ midruleev&841.80&135.10&0.70及以上305.20&449.30&0.70 \\顺畅804.40&106.20及以上0.10及以上707.10&119.90&0.10 \\\ bottomrule\ end {表格}\茶几}
更改 addtorow $ pos< -list(0)
并不是一个答案,因为它正确放置了顶部规则,但将多列行放在了表的列名下方.我正在寻找以下输出:
\ begin {table} [ht]\定心\ begin {tabular} {rrrrrrr}\ toprule&\ multicolumn {3} {c} {样本中}&\ multicolumn {3} {c} {样本外} \\&均值标清最好的均值标清最好的 \\\ midruleev&841.80&135.10&0.70及以上305.20&449.30&0.70 \\平滑&804.40&106.20及以上0.10及以上707.10&119.90&0.10 \\\ bottomrule\ end {表格}\茶几}
任何评论都将不胜感激.
我强烈建议使用令人惊叹的
I want to convert a data.frame into a (booktab) latex table with multicolumns but I fail to create output which puts the \toprule
on top of the table. The following data is used
dat <- structure(c(841.8, 804.4, 135.1, 106.2, 0.7025, 0.09645, 305.2,
707.1, 449.3, 119.9, 0.7025, 0.09645), .Dim = c(2L, 6L), .Dimnames = list(
c("ev", "smooth"), c("Mean", "SD", "best", "Mean", "SD",
"best")))
> dat
Mean SD best Mean SD best
ev 841.8 135.1 0.70250 305.2 449.3 0.70250
smooth 804.4 106.2 0.09645 707.1 119.9 0.09645
addtorow <- list()
addtorow$pos <- list(-1)
addtorow$command <- '& \\multicolumn{3}{c}{Tab a}& \\multicolumn{3}{c}{Tab b}\\\\'
print(xtable(dat), add.to.row=addtorow, include.colnames=TRUE,booktabs=TRUE)
The output looks almost correct, but the \toprule
is on the wrong position.
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrr}
& \multicolumn{3}{c}{In-sample}& \multicolumn{3}{c}{Out-of-sample}\\
\toprule
& Mean & SD & best & Mean & SD & best \\
\midrule
ev & 841.80 & 135.10 & 0.70 & 305.20 & 449.30 & 0.70 \\
smooth & 804.40 & 106.20 & 0.10 & 707.10 & 119.90 & 0.10 \\
\bottomrule
\end{tabular}
\end{table}
Changing addtorow$pos<-list(0)
is not an answer as it places the top rule correctly but puts the multicolumn row below the column-names of the table. I am looking for the following output:
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrr}
\toprule
& \multicolumn{3}{c}{In-sample}& \multicolumn{3}{c}{Out-of-sample}\\
& Mean & SD & best & Mean & SD & best \\
\midrule
ev & 841.80 & 135.10 & 0.70 & 305.20 & 449.30 & 0.70 \\
smooth & 804.40 & 106.20 & 0.10 & 707.10 & 119.90 & 0.10 \\
\bottomrule
\end{tabular}
\end{table}
Any comment is greatly appreciated.
I strongly recommend to use the amazing kableExtra
package.
\documentclass{article}
\usepackage{booktabs}
\begin{document}
<<setup, include=FALSE>>=
library(knitr)
opts_chunk$set(echo=FALSE)
library(kableExtra)
options(knitr.table.format = "latex")
dat <- structure(c(841.8, 804.4, 135.1, 106.2, 0.7025, 0.09645, 305.2,
707.1, 449.3, 119.9, 0.7025, 0.09645), .Dim = c(2L, 6L), .Dimnames = list(
c("ev", "smooth"), c("Mean", "SD", "best", "Mean", "SD",
"best")))
@
<<results='asis'>>=
kable(dat, booktabs = TRUE, caption = "My table", align = "c") %>%
add_header_above(c(" ", "Tab a"=3, "Tab b"=3)) %>%
kable_styling(latex_options = "hold_position")
@
\end{document}
这篇关于带多列和书签的R xtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!