Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Non-onerous method of getting CrossTable (gmodels) results nicely formatted (html) into markdown document

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.

like image 406
Konrad Avatar asked Nov 22 '25 00:11

Konrad


1 Answers

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.

like image 192
daroczig Avatar answered Nov 23 '25 19:11

daroczig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!