我可以在 R 中使用 booktabs 包吗?我正在使用 RStudio,但找不到安装 booktabs 包版本的方法。
我尝试在 .Rmd 文件中呈现的表格如下所示:
%----- Requires booktabs package -----%
\usepackage{booktabs}
\begin{table}[h]
\centering
\caption{Descriptive Statistics}
\label{tab:descriptiveStatistics}
{
\begin{tabular}{lr}
\toprule
& cholesterol \\
\cmidrule[0.4pt]{1-2}
Valid & 100 \\
Missing & 0 \\
Mean & 5.421 \\
Std. Deviation & 0.524 \\
Minimum & 4.364 \\
Maximum & 6.550 \\
\bottomrule
\end{tabular}
}
\end{table}
答案1
您必须\usepackage{booktabs}
在 YAML 标头中使用,以便它以 LaTeX 文件名结尾,而不是在文档正文中,例如使用键header-includes
,例如:
---
header-includes: \usepackage{booktabs}
output: pdf_document
---
Example:
\begin{tabular}{c}
\toprule a\\
\end{tabular}
话虽如此,这是使用 Rmarkdown 制作描述统计表的错误方法:
---
output: pdf_document
---
```{r,echo=F,results='asis', warning=FALSE}
x <- data.frame(cholesterol=rnorm(100,5.4,0.5)) # fake data
z <- data.frame(
Choresterol= c(sum(!is.na(x$cholesterol)),
sum(is.na(x$cholesterol)),
min(x$cholesterol),
max(x$cholesterol),
mean(x$cholesterol),
sd(x$cholesterol)))
rownames(z) <- c("Valid","Missing","Minimun","Maximum","Mean", "Std. Dev.")
options(xtable.comment = FALSE)
print(xtable::xtable(z,caption="Descriptive Statistics",align="lr"),
booktabs=T,caption.placement="top")
```
```{r,echo=F,message=FALSE}
# simplest alternative
knitr::kable(summary(x), booktabs = T, caption="summary")
# more detalied alternative
knitr::kable(round(summarytools::descr(x),2), booktabs = T, caption="descr")
```
请注意,这里使用 booktabs 时没有在 YAML 标头中添加任何内容。