the code below
---
title: "Example"
output: html_document
---
```{r}
require(datasets)
data(esoph)
require(knitr, quietly = TRUE, warn.conflicts = FALSE)
kable(table(esoph$agegp, esoph$alcgp),
caption = "Some sample")
```
Generates this pleasant table:
I like more the results generated by the CrossTable
function from the gmodels
package, where I have a lot flexibility to introduce interesting summaries:
> CrossTable(esoph$agegp, esoph$alcgp, digits = 1, prop.r = FALSE, prop.t = FALSE, chisq = FALSE,
+ prop.chisq = FALSE)
Cell Contents
|-------------------------|
| N |
| N / Col Total |
|-------------------------|
========================================================
esoph$alcgp
esoph$agegp 0-39g/day 40-79 80-119 120+ Total
--------------------------------------------------------
25-34 4 4 3 4 15
0.2 0.2 0.1 0.2
--------------------------------------------------------
35-44 4 4 4 3 15
0.2 0.2 0.2 0.1
--------------------------------------------------------
45-54 4 4 4 4 16
0.2 0.2 0.2 0.2
--------------------------------------------------------
55-64 4 4 4 4 16
0.2 0.2 0.2 0.2
--------------------------------------------------------
65-74 4 3 4 4 15
0.2 0.1 0.2 0.2
--------------------------------------------------------
75+ 3 4 2 2 11
0.1 0.2 0.1 0.1
--------------------------------------------------------
Total 23 23 21 21 88
0.3 0.3 0.2 0.2
========================================================
What I would like to achieve is to use the results generated via the CrossTable
in the same manner as I'm using the table results. In particular, I'm interested in forcing those results in the kable
so it formats nicely. I browser through StackOverflow and came across the relevant discussions on Printing cross-tabulations in knitr or introducing counts and percentages in xtables. The solutions discussed there appear to be elaborate. In effect, I would only like to move my CrossTable
output to an object that kable
can understand reformat.