Sweave 中的 xtable:在 table + 对齐中指定 toprule 或 midrule

xtable in Sweave: specify toprule or midrule in a table + alignement

我正在使用 R 中的 Sweave 生成​​自动报告。我想要:

  1. 在位置 (nrow -1) 我想要一个 toprule 而不是 midrule
  2. 将第一行居中对齐,然后我想将数字右对齐以便对齐数字。

这是我的代码:

\documentclass[a4paper,18pt]{article}
\usepackage[top=2.5cm, left=2.5cm, right=2.5cm,bottom=2.5cm]{geometry}
\usepackage{Sweave} 
\usepackage{booktabs}
\makeatletter
  \def\hrulefill#1{\leavevmode\leaders\hrule\@height#1\hfill \kern\z@}
\makeatother
\begin{document}
\SweaveOpts{concordance=TRUE}

<<xtable1, results=tex,echo=FALSE>>=
library(xtable)

> data
          l w  a m
x     NA  515.0 0.22 127.83
y     NA   75.0 0.45  33.75
z     NA   85.0 0.45  38.25


xtab <- xtable(data)
hline <- c(-1,0,nrow(xtab)-1,nrow(xtab))
align(xtab) <- "lcccc"
print(xtab,booktabs=T,hline.after=hline)
@


\end{document} 

documentation 开始,结合使用 add.to.rowhline.after=NULL 来控制向 hline 向量中的行添加的内容。

library(xtable)
xtab <- xtable(data)

hline <- c(-1,0,nrow(xtab)-1,nrow(xtab))
htype <- c("\toprule ", "\midrule ", "\toprule ","\bottomrule ")

print(xtab,add.to.row = list(pos = as.list(hline),
                             command = htype),
      hline.after = NULL)

数据

data <- structure(list(l = c(NA, NA, NA), w = c(515, 75, 85), a = c(0.22, 
0.45, 0.45), m = c(127.83, 33.75, 38.25)), class = "data.frame", row.names = c("x", "y", "z"))