---
title: Combining flattened contingency tables (and other tables)
output: rmarkdown::html_vignette
vignette: >
  % \VignetteIndexEntry{Combining flattened contingency tables (and other tables)}
  % \VignetteEngine{knitr::rmarkdown}
  % \VignetteEncoding{UTF-8}
---

```{r,echo=FALSE,message=FALSE}
knitr::opts_chunk$set(comment=NA,
               fig.align="center",
               results="markup")
```

This vignette demonstrates how flattened contingency tables can be combined into more
complex tables. For starters, we create a few contingendy tables.

```{r}
tab.Class.Age <- xtabs(Freq~Class+Age,data=Titanic)
tab.Survived.Class.Age <- xtabs(Freq~Survived+Class+Age,data=Titanic)
tab.Survived.Class.Sex <- xtabs(Freq~Survived+Class+Sex,data=Titanic)
tab.Survived.Class <- xtabs(Freq~Survived+Class,data=Titanic)
tab.Survived.Sex <- xtabs(Freq~Survived+Sex,data=Titanic)
tab.Survived.Age <- xtabs(Freq~Survived+Age,data=Titanic)
tab.Survived <- xtabs(Freq~Survived,data=Titanic)
```
Then we load the *memisc* package:
```{r,message=FALSE}
library(memisc)
```

Next, we create a few flattened contingency tables:
```{r}
(ftab.Survived.Age <- ftable(tab.Survived.Age))
(ftab.Survived.Sex <- ftable(tab.Survived.Sex))
```
Since these tables have the same row variables, we can combine them columnwise:
```{r}
cbind(ftab.Survived.Age,
      ftab.Survived.Sex)
```
We can even add a simple table of counts:
```{r}
cbind(ftab.Survived.Age,
      ftab.Survived.Sex,
      Total=tab.Survived)
```
Of course, it is not enough to see such tables on screen, we also may need them in a presentable
format, such as LaTeX or HTML. For the latter format we again have `format_html` and `show_html`.
To see how this works, we first look at the individual `ftable`s:
```{r,results='asis'}
show_html(ftab.Survived.Age)
```
```{r,results='asis'}
show_html(ftab.Survived.Sex)
```
... and then at their combination
```{r,results='asis'}
show_html(
  cbind(ftab.Survived.Age,
      ftab.Survived.Sex,
      Total=tab.Survived)
)
```
To make, in "knitr", the creation of HTML-versions automatic one can use the following little trick:
```{r}
knit_print.ftable_matrix <-function(x,options,...)
  knitr::asis_output(
    format_html(x,
                digits=if(length(options$ftable.digits))
                          options$ftable.digits
                       else 0,
                ...))
```
Now we do not need to call `show_html` while using "knitr":
```{r}
cbind(ftab.Survived.Age,
      ftab.Survived.Sex,
      Total=tab.Survived)
```
This can be undone by removing the method function of `knit_print`:
```{r}
rm(knit_print.ftable_matrix)
```
For `show_html` and `toLatex` there are some variants in how the 
variable names are positioned, for example:
```{r,results='asis'}
show_html(
  cbind(ftab.Survived.Age,
      ftab.Survived.Sex,
      Total=tab.Survived),
  varinfront=FALSE
)
```

```{r,results='asis'}
show_html(
  cbind(ftab.Survived.Age,
      ftab.Survived.Sex,
      Total=tab.Survived),
  varontop=FALSE
)
```
Of course it is also possible to combine flat contingency tables rowwise:
```{r}
ftab.Age.Survived <- ftable(tab.Survived.Age,col.vars=1)
ftab.Sex.Survived <- ftable(tab.Survived.Sex,col.vars=1)
ftab.Class.Survived <- ftable(tab.Survived.Class,col.vars=1)

rbind(
  ftab.Age.Survived,
  ftab.Sex.Survived,
  ftab.Class.Survived,
  Total=tab.Survived
)
```
```{r,results='asis'}
show_html(
  rbind(
    ftab.Age.Survived,
    ftab.Sex.Survived,
    ftab.Class.Survived,
    Total=tab.Survived
  )
)
```
It is also possible to create the `ftable`s from tables of percentages or generic tables (created
with `genTable`) etc.
```{r,results='asis'}
ptab.Survived.Age<-percentages(Survived~Age,data=Titanic)
ptab.Survived.Sex<-percentages(Survived~Sex,data=Titanic)
ptab.Survived.Class<-percentages(Survived~Class,data=Titanic)

fptab.Age.Survived <- ftable(ptab.Survived.Age,col.vars=1)
fptab.Sex.Survived <- ftable(ptab.Survived.Sex,col.vars=1)
fptab.Class.Survived <- ftable(ptab.Survived.Class,col.vars=1)

show_html(
  rbind(
    fptab.Age.Survived,
    fptab.Sex.Survived,
    fptab.Class.Survived
  ),
  digits=1
)
```

It is also possible to combine `rbind` and `cbind`:
```{r,results='asis'}
tab.Age <- xtabs(Freq~Age,data=Titanic)
tab.Sex <- xtabs(Freq~Sex,data=Titanic)
tab.Class <- xtabs(Freq~Class,data=Titanic)


show_html(
  rbind(
    cbind(fptab.Age.Survived,Total=tab.Age),
    cbind(fptab.Sex.Survived,Total=tab.Sex),
    cbind(fptab.Class.Survived,Total=tab.Class)
  ),
  digits=c(1,0) # One digit after dot for percentages 
                # no digits for total counts.
)
```
The same construct as LaTeX code:
```{r}
toLatex(
  rbind(
    cbind(fptab.Age.Survived,Total=tab.Age),
    cbind(fptab.Sex.Survived,Total=tab.Sex),
    cbind(fptab.Class.Survived,Total=tab.Class)
  ),
  digits=c(1,0) # One digit after dot for percentages 
                # no digits for total counts.
)
```