gpt4 book ai didi

r - 如何向 R 中的 tabular{tables} 对象添加额外的水平线?

转载 作者:行者123 更新时间:2023-12-04 09:23:27 25 4
gpt4 key购买 nike

我想添加一个额外的 \hline到我使用 tabular(){tables} 在 R sweave 文档中创建的复杂表格。

以 Iris 数据为例:我想在 Iris setosa 下方添加一条额外的水平线,仅跨越列,而不跨越行名称。目前,我有这个:

Example of a table with multi-coloum header

生成表的代码:

\begin{table}
\begin{center}
<<iristable, eval=TRUE, echo=FALSE, results='asis', message=FALSE>>=
library(Hmisc) ; library(tables)


tab.obj <- tabular( Species ~ (Heading("Mean")*
(Heading("")*mean*Sepal.Width +
Heading("")*mean*Sepal.Length)) +
(Heading("Median")*
(Heading("")*median*Sepal.Width +
Heading("")*median*Sepal.Length)),
data=iris)

nicetable<- booktabs() ## needs LaTex package \usepackage{booktabs}
table_options(nicetable)
table_options(titlerule="\\cmidrule(lr)")

latex(tab.obj)
@
\caption{This table is just an example.}
\end{center}
\label{tab:example_table}
\end{table}

我想这一定是可能的,但我不知道怎么做。

最佳答案

查看函数 tables:::latex.tabular有一种方法可以计算 titlerule 的 cline 参数,

   titlerules <- sprintf("%s%s{%d-%d}", titlerules, 
opts$titlerule, firstcol, firstcol +
ncols - 1)

但不适用于中间规则。所以你必须手动完成......这里使用

table_options(midrule="\cmidrule(lr){2-3}\cmidrule(lr){4-5}")


\begin{table}
\begin{center}
<<iristable, eval=TRUE, echo=FALSE, results='asis', message=FALSE>>=
library(Hmisc) ; library(tables)
tab.obj <- tabular( Species ~ (Heading("Mean")*
(Heading("")*mean*Sepal.Width +
Heading("")*mean*Sepal.Length)) +
(Heading("Median")*
(Heading("")*median*Sepal.Width +
Heading("")*median*Sepal.Length)),
data=iris)

nicetable<- booktabs() ## needs LaTex package \usepackage{booktabs}
table_options(nicetable)
table_options(titlerule="\\cmidrule(lr)")
table_options(midrule="\\cmidrule(lr){2-3}\\cmidrule(lr){4-5}")
latex(tab.obj)
@
\caption{This table is just an example.}
\end{center}
\label{tab:example_table}
\end{table}

midrule_cline

关于r - 如何向 R 中的 tabular{tables} 对象添加额外的水平线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com