I'm printing frequency tables using knit::kable
and pander::pandoc
, and generally this works great for HTML/Word/LaTeX output. But sometimes I'd like to preserve dimension names in the final product. Unfortunately, both pander
and knitr
drop these when converting to markdown.
# create a simple table
tab <- table(mtcars$gear, mtcars$carb)
# add dimension names
names(dimnames(tab)) <- c("gear", "carb")
This creates a table:
carb
gear 1 2 3 4 6 8
3 3 4 3 5 0 0
4 4 4 0 4 0 0
5 0 2 0 1 1 1
But now if we print with, say, kable:
> kable(tab)
| | 1| 2| 3| 4| 6| 8|
|:--|--:|--:|--:|--:|--:|--:|
|3 | 3| 4| 3| 5| 0| 0|
|4 | 4| 4| 0| 4| 0| 0|
|5 | 0| 2| 0| 1| 1| 1|
No dimension names! (And ?kable
does not indicate any option that would include them.)
Any suggestions for a tool that will preserve these? I've noticed that descr:CrossTable
does the trick, but includes a whole lot of extra info I'd like to omit.
Many thanks.
You can use eg ftable
to create a flat contingency table implicitly having the dimension names:
> pander::pander(ftable(tab))
---- ---- - - - - - -
carb 1 2 3 4 6 8
gear
3 3 4 3 5 0 0
4 4 4 0 4 0 0
5 0 2 0 1 1 1
---- ---- - - - - - -
Or you can also suppress the not needed cells from descr::CrossTable
such as:
> pander(descr::CrossTable(tab, prop.r = FALSE, prop.c = FALSE, prop.chisq = FALSE))
------------------------------------------------------------------------------
\ carb\ \ \ \ \ \ \
gear 1 2 3 4 6 8 Total
--------- -------- --------- --------- --------- --------- --------- ---------
**3**\ \ \ \ \ \ \ \
N\ 3\ 4\ 3\ 5\ 0\ 0\ 15\
Total(%) 9.375% 12.500% 9.375% 15.625% 0.000% 0.000%
**4**\ \ \ \ \ \ \ \
N\ 4\ 4\ 0\ 4\ 0\ 0\ 12\
Total(%) 12.500% 12.500% 0.000% 12.500% 0.000% 0.000%
**5**\ \ \ \ \ \ \ \
N\ 0\ 2\ 0\ 1\ 1\ 1\ 5\
Total(%) 0.000% 6.250% 0.000% 3.125% 3.125% 3.125%
Total 7 10 3 10 1 1 32
------------------------------------------------------------------------------
Or submit a ticket on GH :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With