如何使用 R 的 xtable() 打印 table() 的输出并保留行和列边距标题?并旋转

如何使用 R 的 xtable() 打印 table() 的输出并保留行和列边距标题?并旋转

我需要从不同的向量对生成许多小表,如下所示:(向量具有不同长度的名称)

library(xtable)
input1 <- c(0,0,0,1,1,1,1,2,2,2,2)
input2 <- c(0,0,0,0,0,1,0,0,0,1,2)
result <- table(input1, input2)
xtable(result)
   or 
print(xtable(x), include.rownames=T, include.colnames=T)

控制台输出正常。

结果

       input2
input1 0 1 2
     0 3 0 0
     1 3 1 0
     2 2 1 1

但是我想获得一个漂亮的乳胶输出。xtable
生成以下代码:

\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
  \hline
 & 0 & 1 & 2 \\ 
  \hline
  0 &   3 &   0 &   0 \\ 
  1 &   3 &   1 &   0 \\ 
  2 &   2 &   1 &   1 \\ 
   \hline
\end{tabular}
\end{table}

如您所见,“input1”和“input2”边距标题已经消失。 在此处输入图片描述

但我想要得到这样的东西:(或者如果你认为它看起来更好,可以有更多线条)。 在此处输入图片描述

我需要在 R 中使用什么命令才能获得它?

我找到了一个解决方案,至少可以显示名称,但方式并不美观:

print(xtable(format(ftable(result))), include.rownames=FALSE, include.colnames=FALSE, sanitize.text.function = function(x) {gsub('"',"",x)})

\begin{table}[ht]
\centering
\begin{tabular}{lllll}
  \hline
  \hline
         & input2 & 0 & 1 & 2 \\ 
  input1 &          &     &     &     \\ 
  0      &          &   3 &   0 &   0 \\ 
  1      &          &   3 &   1 &   0 \\ 
  2      &          &   2 &   1 &   1 \\ 
   \hline
\end{tabular}
\end{table}

在此处输入图片描述

使用此方法,左列的分隔将取决于其标题的长度。我更希望将左标题旋转并放置在左侧,将另一个标题放置在顶部。

我需要知道如何以编程方式修改 R 生成的 LaTeX 输出。

答案1

强制将 R 的 dinnamestable()设置为多列和旋转多行对我来说似乎并不简单,所以这不是你要问的,但也许值得展示一些替代格式。我认为获得的结果xtableFtable更容易理解:

tbl <-ftable(result,row.vars=c(1,2))
xftbl <- xtableFtable(tbl, method = "compact")
print.xtableFtable(xftbl, booktabs = T) 

mwe1

其他一些...

姆韦

---
output:
  pdf_document: default
header-includes:
- \usepackage{diagbox}
- \usepackage{booktabs}
- \usepackage{rotating}
- \usepackage{multicol}
---
\parskip1em\centering

```{r, echo=F,results='asis',warning=F,message=F}
library(xtable)
input1 <- c(0,0,0,1,1,1,1,2,2,2,2)
input2 <- c(0,0,0,0,0,1,0,0,0,1,2)
result <- table(input1, input2)
result1 <- data.frame(input1, input2)
options(omitlatexcom=T,xtable.comment=F,xtable.floating=F)
```


```{r, echo=F,results='asis',warning=F,message=F}
Hmisc::latex(result,comment=F,rowlabel="\\diagbox{input1}{input2}",  file="") 
```

```{r, echo=F,results='asis',warning=F,message=F}
xtableFtable(ftable(result1)) 
```


```{r, echo=F,results='asis',warning=F,message=F}
tbl <-ftable(result)
xftbl <- xtableFtable(tbl, method = "compact")
print.xtableFtable(xftbl, booktabs = F)
```

```{r, echo=F,results='asis',warning=F,message=F}
tbl <-ftable(result,row.vars=c(1,2))
xftbl <- xtableFtable(tbl, method = "compact")
print.xtableFtable(xftbl, booktabs = T, rotate.rownames = T)
```

```{r, echo=F,results='asis',warning=F,message=F}
x <- as.data.frame(result)
knitr::kable(x)
```

```{r, echo=F,results='asis',warning=F,message=F}
x <- format(ftable(result))
# knitr::kable(x)
```

答案2

适当使用\multirow\multicolumn您可以实现以下目标:

在此处输入图片描述

\documentclass{article}
\usepackage{multirow} % For the multirow command
\usepackage{graphicx} % For the rotatebox command 
\begin{document}

\begin{table}[ht]
\centering
\begin{tabular}{ll|lll}
  & \multicolumn{1}{l}{}  & \multicolumn{3}{c}{input2}\\
  & \multicolumn{1}{l}{}  &   0 &   1 &   2 \\  
    \cline{3-5}
\multirow{3}{*}{\rotatebox[origin=c]{90}{input1}}  & 0 &   3 &   0 &   0 \\ 
  & 1 &   3 &   1 &   0 \\ 
  & 2 &   2 &   1 &   1 \\ 
\end{tabular}
\end{table}

\end{document}

间距\begin{tabular}{l@{\hspace{0.75\tabcolsep}}l|lll}更紧密:

在此处输入图片描述

答案3

如上所述(以学生表为例)。

使用 R

library(Hmisc)
n <-seq(1,10,by=1)
alpha <- c(0.5,0.4,0.3,0.2,0.1,0.05,0.02,0.01,0.001)
mt<-outer(n,alpha, function(n,alpha) round(qt(1-alpha/2,n),2) )
colnames(mt)<-alpha
rownames(mt)<-n
Hmisc::latex(mt,comment=F,rowlabel="\\diagbox{$\\nu$}{$\\alpha$}",  file="")

由此得出以下结论结果(在 R 中):

\begin{table}[!tbp]
\begin{center}
\begin{tabular}{lrrrrrrrrr}
\hline\hline
\multicolumn{1}{l}{\diagbox{$\nu$}{$\alpha$}}&\multicolumn{1}{c}{0.5}&\multicolumn{1}{c}{0.4}&\multicolumn{1}{c}{0.3}&\multicolumn{1}{c}{0.2}&\multicolumn{1}{c}{0.1}&\multicolumn{1}{c}{0.05}&\multicolumn{1}{c}{0.02}&\multicolumn{1}{c}{0.01}&\multicolumn{1}{c}{0.001}\tabularnewline
\hline
1&$1.00$&$1.38$&$1.96$&$3.08$&$6.31$&$12.71$&$31.82$&$63.66$&$636.62$\tabularnewline
2&$0.82$&$1.06$&$1.39$&$1.89$&$2.92$&$ 4.30$&$ 6.96$&$ 9.92$&$ 31.60$\tabularnewline
3&$0.76$&$0.98$&$1.25$&$1.64$&$2.35$&$ 3.18$&$ 4.54$&$ 5.84$&$ 12.92$\tabularnewline
4&$0.74$&$0.94$&$1.19$&$1.53$&$2.13$&$ 2.78$&$ 3.75$&$ 4.60$&$  8.61$\tabularnewline
5&$0.73$&$0.92$&$1.16$&$1.48$&$2.02$&$ 2.57$&$ 3.36$&$ 4.03$&$  6.87$\tabularnewline
6&$0.72$&$0.91$&$1.13$&$1.44$&$1.94$&$ 2.45$&$ 3.14$&$ 3.71$&$  5.96$\tabularnewline
7&$0.71$&$0.90$&$1.12$&$1.41$&$1.89$&$ 2.36$&$ 3.00$&$ 3.50$&$  5.41$\tabularnewline
8&$0.71$&$0.89$&$1.11$&$1.40$&$1.86$&$ 2.31$&$ 2.90$&$ 3.36$&$  5.04$\tabularnewline
9&$0.70$&$0.88$&$1.10$&$1.38$&$1.83$&$ 2.26$&$ 2.82$&$ 3.25$&$  4.78$\tabularnewline
10&$0.70$&$0.88$&$1.09$&$1.37$&$1.81$&$ 2.23$&$ 2.76$&$ 3.17$&$  4.59$\tabularnewline
\hline
\end{tabular}\end{center}
\end{table}

放在结果在 LaTeX 中

\documentclass{article}
\usepackage{diagbox}
\begin{document}
% result here
\begin{table}[!tbp]
\begin{center}
\begin{tabular}{lrrrrrrrrr}
\hline\hline
\multicolumn{1}{l}{\diagbox{$\nu$}{$\alpha$}}&\multicolumn{1}{c}{0.5}&\multicolumn{1}{c}{0.4}&\multicolumn{1}{c}{0.3}&\multicolumn{1}{c}{0.2}&\multicolumn{1}{c}{0.1}&\multicolumn{1}{c}{0.05}&\multicolumn{1}{c}{0.02}&\multicolumn{1}{c}{0.01}&\multicolumn{1}{c}{0.001}\tabularnewline
\hline
1&$1.00$&$1.38$&$1.96$&$3.08$&$6.31$&$12.71$&$31.82$&$63.66$&$636.62$\tabularnewline
2&$0.82$&$1.06$&$1.39$&$1.89$&$2.92$&$ 4.30$&$ 6.96$&$ 9.92$&$ 31.60$\tabularnewline
3&$0.76$&$0.98$&$1.25$&$1.64$&$2.35$&$ 3.18$&$ 4.54$&$ 5.84$&$ 12.92$\tabularnewline
4&$0.74$&$0.94$&$1.19$&$1.53$&$2.13$&$ 2.78$&$ 3.75$&$ 4.60$&$  8.61$\tabularnewline
5&$0.73$&$0.92$&$1.16$&$1.48$&$2.02$&$ 2.57$&$ 3.36$&$ 4.03$&$  6.87$\tabularnewline
6&$0.72$&$0.91$&$1.13$&$1.44$&$1.94$&$ 2.45$&$ 3.14$&$ 3.71$&$  5.96$\tabularnewline
7&$0.71$&$0.90$&$1.12$&$1.41$&$1.89$&$ 2.36$&$ 3.00$&$ 3.50$&$  5.41$\tabularnewline
8&$0.71$&$0.89$&$1.11$&$1.40$&$1.86$&$ 2.31$&$ 2.90$&$ 3.36$&$  5.04$\tabularnewline
9&$0.70$&$0.88$&$1.10$&$1.38$&$1.83$&$ 2.26$&$ 2.82$&$ 3.25$&$  4.78$\tabularnewline
10&$0.70$&$0.88$&$1.09$&$1.37$&$1.81$&$ 2.23$&$ 2.76$&$ 3.17$&$  4.59$\tabularnewline
\hline
\end{tabular}\end{center}
\end{table}
\end{document}

在此处输入图片描述

相关内容