0

I need to generate many small tables from different pairs of vectors like this: (the vectors have names of different lengths)

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)

The console output is OK.

result

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

But I'd like to get a nice latex output instead.
xtable produces this code:

\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}

As you can see the "input1" and "input2" margin titles have dissapeared. enter image description here

But I'd like to get something like this: (or with more lines if you think it looks nicer). enter image description here

What command do I need to use in R in order to get it?

I've found a solution that at least shows the names, but not in a beautiful way:

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}

enter image description here

with this method the separation of the left column will depend on the length of its title. I would prefer to have the left title rotated and placed to the left, and the other one on the top.

I need to know how to programatically modify the generated LaTeX output from R.

3 Answers 3

4

Force the dinnames of a R table() to a multicolumn and a rotated multirow seem not trivial for me, so this is not what you ask, but maybe worth show some an alternative format. I think that the obtained with xtableFtable is even more understandable:

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

mwe1

Some others ...

mwe

---
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

With appropriate use of \multirow and \multicolumn you can achieve the following:

enter image description here

\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}

With \begin{tabular}{l@{\hspace{0.75\tabcolsep}}l|lll} the spacing is tighter:

enter image description here

6
  • But i don't mean writing the code by hand on the latex editor but getting it directly from R. Maybe doing it in general could be difficult, we could particularize it for a given dimension, for example 3x3 tables.
    – skan
    Sep 5, 2019 at 10:57
  • @skan: Sorry for this misundestanding. Maybe you can find something more useful here: possible to create latex multicolumns in xtable?
    – leandriis
    Sep 5, 2019 at 11:54
  • Another simple question. How can I get the input1 label a little bit closer to the table? (more to the right)
    – skan
    Sep 5, 2019 at 22:30
  • @skan: I've updated my answer on that.
    – leandriis
    Sep 6, 2019 at 7:14
  • 1
    @skan: With \begin{tabular}{l@{}l|lll} & \multicolumn{1}{l}{} & \multicolumn{3}{c}{input2}\\ & \multicolumn{1}{l}{} & 0 & 1 & 2 \\ \cline{3-5} \multirow{3}{*}{\rotatebox[origin=c]{90}{\thead{input1\\ more\\ text}}} & 0 & 3 & 0 & 0 \\ & 1 & 3 & 1 & 0 \\ & 2 & 2 & 1 & 1 \\ \end{tabular} you get i.stack.imgur.com/JyYXG.png without the need to manually adjust the width of the respective column.
    – leandriis
    Sep 6, 2019 at 14:02
2

As discussed above (example for a student table).

With 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="")

This gives the following result (in 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}

Put the result in 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}

enter image description here

2
  • 1
    Welcome to Tex.SE! I would suggest you include what you label as the "result" in your answer. Nov 24, 2022 at 11:46
  • Done. Thanks for the advice.
    – 2WFR
    Nov 27, 2022 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .