[Tex/LaTex] How to use R’s xtable() to print the output of table() and keep the row and column margin titles? and rotate

rtables

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.

Best Answer

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)
```
Related Question