[R] Formatted contingency tables with (%)

Marc Schwartz marc_schwartz at me.com
Wed Nov 11 00:23:19 CET 2009


On Nov 10, 2009, at 2:36 PM, David Winsemius wrote:

>
> On Nov 10, 2009, at 3:07 PM, soeren.vogel at eawag.ch wrote:
>
>> Quite often, I need those tables:
>>
>> x <- sample(c("a", "b", "c"), 40, rep=T)
>> y <- sample(c("X", "Y"), 40, rep=T)
>> (tbl <- table(x, y))
>> (z <- as.factor(paste(as.vector(tbl), " (",  
>> round(prop.table(as.vector(tbl)) * 100, 1), "%)", sep="")))
>> matrix(as.factor(z), nrow=3, dimnames=dimnames(tbl))
>>
>> But the result looks ugly and is not copy&paste-able for LaTeX  
>> verbatim or table environment, moreover, the "\"" is not what I  
>> want in the printout. How to achieve:
>>
>>  y
>> x  X          Y
>> a  3  (7.5%)   7 (17.5%)
>> b  9 (22.5%)   5 (12.5%)
>> c  6 (15.0%)  10 (25.0%)
>>
>> Thank you for help or hints.
>
>
> > library(gmodels)
> >> CrossTable(tbl, prop.c=F, prop.r=F, prop.chisq=F)  # author, Marc  
> Schwartz
>
>
>   Cell Contents
> |-------------------------|
> |                       N |
> |         N / Table Total |
> |-------------------------|
>
>
> Total Observations in Table:  40
>
>
>             | y
>           x |         X |         Y | Row Total |
> -------------|-----------|-----------|-----------|
>           a |         6 |        10 |        16 |
>             |     0.150 |     0.250 |           |
> -------------|-----------|-----------|-----------|
>           b |        12 |         4 |        16 |
>             |     0.300 |     0.100 |           |
> -------------|-----------|-----------|-----------|
>           c |         3 |         5 |         8 |
>             |     0.075 |     0.125 |           |
> -------------|-----------|-----------|-----------|
> Column Total |        21 |        19 |        40 |
> -------------|-----------|-----------|-----------|


Thanks for the attribution David.

Just as an FYI, Jakson Aquino has taken the CrossTable() code from  
gmodels, incorporated it into his own CRAN package called 'descr' and  
has created some wrappers for it to enable some additional  
functionality. Importantly, he is working on print methods that would  
leverage the functionality of the xtable package to be able to output  
the CrossTable() tabular content to either LaTeX or HTML. I don't have  
any plans to further enhance the function, so fully support his  
efforts and of course the CrossTable() code is GPL, which enables this.

A quick check of the package shows that at present, it will output the  
table counts in a fashion similar to what David has included in his  
second e-mail on this subject. Perhaps in future iterations, he will  
include other components of the CrossTable output.

The CrossTable output as David has above will work within a LaTeX  
verbatim environment, where a monospace font is used, so that spacing  
is appropriate. As David has shown in his second e-mail, one can  
construct a table and use xtable to generate a nice looking tabular  
environment.

Additionally, I would point to Frank's latex() function in the Hmisc  
package as another option for generated LaTeX tabulars:

For simple output such as this, it would not overly difficult to  
construct a function to output the counts and percentages to a  
tabular. Keep in mind that the '%' symbol is of course a LaTeX comment  
character, so one would have to escape it so that it is interpreted as  
a percent symbol and not as a comment prefix. In R, that means using "\ 
\%" so that the result output for inclusion in a TeX file is "\%" when  
using cat().

HTH,

Marc Schwartz




More information about the R-help mailing list