Keep table dimnames in knitr or pander output?
Asked Answered
A

1

9

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.

Abhor answered 31/12, 2015 at 19:56 Comment(0)
O
8

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

------------------------------------------------------------------------------
 &nbsp;\   carb\    &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\ 
  gear       1         2         3         4         6         8       Total  
--------- -------- --------- --------- --------- --------- --------- ---------
 **3**\   &nbsp;\  &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\  
   N\       3\       4\        3\        5\        0\        0\         15\   
Total(%)   9.375%   12.500%   9.375%    15.625%   0.000%    0.000%            

 **4**\   &nbsp;\  &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\  
   N\       4\       4\        0\        4\        0\        0\         12\   
Total(%)  12.500%   12.500%   0.000%    12.500%   0.000%    0.000%            

 **5**\   &nbsp;\  &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\  
   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 :)

Overactive answered 31/12, 2015 at 22:29 Comment(4)
Worked perfectly! This is so simple that I can't believe I didn't try it before coming to SO for help. Thanks!Abhor
One more thing--I just noticed that the output for pander::pander(ftable(tab)) in your answer does not have quotes around any of the column, row, or dimension names. But when I run this same command (Pander 0.6.0 on R 3.2.2 for OS X 10.11.2), everything is wrapped in double quotes. Any idea how to suppress this? Thanks again.Abhor
@Abhor shouldn't happen with recent version of pander -- can you please submit a ticket on GH including devtools::session_info() and a full reproducible example (like the lines above creating tab and then the pander + ftable call? That would be very helpful.Overactive
Good to know. Will submit a ticket, @daroczig. Thanks again for the help!Abhor

© 2022 - 2024 — McMap. All rights reserved.