3

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: Table example - kable and 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.

Community
  • 1
  • 1
Konrad
  • 17,740
  • 16
  • 106
  • 167

1 Answers1

7

Give a try to the pander general S3 method instead of kable:

> pander(CrossTable(esoph$agegp, esoph$alcgp, digits = 1))

------------------------------------------------------------
         0-39g/day   40-79    80-119    120+    Total  
------------ ----------- -------- -------- -------- --------
**25-34**\     \     \   \   \   \ 
    N\           4\        4\       3\       4\       15\   
 Row(%)\        27%\      27%\     14%\      5%\      17%   
 Column(%)      27%\      17%\     19%\      3%\            
                 20%       17%       5%       5%            

**35-44**\     \     \   \   \   \ 
    N\           4\        4\       4\       3\       15\   
 Row(%)\        27%\      20%\     19%\      5%\      17%   
 Column(%)      27%\      17%\     14%\      5%\            
                 27%       17%       5%       3%            

**45-54**\     \     \   \   \   \ 
    N\           4\        4\       4\       4\       16\   
 Row(%)\        25%\      25%\     19%\      5%\      18%   
 Column(%)      25%\      17%\     19%\      5%\            
                 25%       17%       5%       5%            

**55-64**\     \     \   \   \   \ 
    N\           4\        4\       4\       4\       16\   
 Row(%)\        25%\      25%\     19%\      5%\      18%   
 Column(%)      25%\      17%\     19%\      5%\            
                 25%       17%       5%       5%            

**65-74**\     \     \   \   \   \ 
    N\           4\        3\       4\       4\       15\   
 Row(%)\        27%\      27%\     19%\      3%\      17%   
 Column(%)      20%\      17%\     19%\      5%\            
                 27%       13%       5%       5%            

 **75+**\      \     \   \   \   \ 
    N\           3\        4\       2\       2\       11\   
 Row(%)\        27%\      18%\     10%\      5%\      12%   
 Column(%)      36%\      13%\     10%\      2%\            
                 18%       17%       3%       2%            

   Total        23\        23\      21\      21\      88\   
                 26%       26%      24%      24%            
------------------------------------------------------------

Although it's not currently supporting suppressing row percentages, but you can suppress column or total percentages natively with CrossTable:

> pander(CrossTable(esoph$agegp, esoph$alcgp, digits = 1, prop.c = FALSE, prop.t = FALSE, chisq = FALSE, prop.chisq = FALSE))

------------------------------------------------------------
         0-39g/day   40-79    80-119    120+    Total  
------------ ----------- -------- -------- -------- --------
**25-34**\     \     \   \   \   \ 
    N\           4\        4\       3\       4\       15\   
   Row(%)        27%       27%      20%      27%      17%   

**35-44**\     \     \   \   \   \ 
    N\           4\        4\       4\       3\       15\   
   Row(%)        27%       27%      27%      20%      17%   

**45-54**\     \     \   \   \   \ 
    N\           4\        4\       4\       4\       16\   
   Row(%)        25%       25%      25%      25%      18%   

**55-64**\     \     \   \   \   \ 
    N\           4\        4\       4\       4\       16\   
   Row(%)        25%       25%      25%      25%      18%   

**65-74**\     \     \   \   \   \ 
    N\           4\        3\       4\       4\       15\   
   Row(%)        27%       20%      27%      27%      17%   

 **75+**\      \     \   \   \   \ 
    N\           3\        4\       2\       2\       11\   
   Row(%)        27%       36%      18%      18%      12%   

   Total        23\        23\      21\      21\      88\   
                 26%       26%      24%      24%            
------------------------------------------------------------

About row-percentage support, please add a comment at the related issue, and I am pretty sure @RomanTsegelskyi will be happy to fix this.

daroczig
  • 28,004
  • 7
  • 90
  • 124
  • Hi, thanks very much for showing the interest. I tried your code but the results are slightly messy (problem `CrossTable` introducing additional rows at the beginning). Screenshots: [1](http://cl.ly/image/0l0m13432G1z),[2](http://cl.ly/image/2Q2m2q3I0y3R). – Konrad Dec 17 '14 at 16:47
  • @Konrad I've just realized you are suing `gmodels::CrossTable` while `pander` supports `descr::CrossTable`. AFAIK it works similarly, so loading `descr` instead of `gmodels` should resolve this issue. If not, then probably you are using the CRAN version of `pander`, can you please give a try with the most [recent version](http://rapporter.github.io/pander/#installation) with `devtools`? – daroczig Dec 17 '14 at 20:14
  • Thank you Sir! It works like a charm, [preview](http://rpubs.com/konradedgar/tablesexample). – Konrad Dec 17 '14 at 20:31
  • Using WINDOWS PANDOC to convert the rmd file shown in this section, I have had trouble getting horizontal and vertical lines to isolate each cell as given in any gmodels CrossTable output. This is what happens after I run PANDOC from the rmd file in this section: * If I create a DOCX file and open it with Libra Office, I get the lines I want on the screen, but not when I print the file. * If I open the same DOCX file in the Office 16 Word, I do not get the lines. * If I create an HTML file, I also do not get the lines. Is there another easy way to get the lines I want? – Bob Sep 10 '21 at 01:49