Skip to content
Back to formatted view

Raw Message

Message-ID: <716F2F9C-542F-41E3-8907-ED3BECE51C6C@eawag.ch>
Date: 2009-11-10T20:07:29Z
From: soeren.vogel at eawag.ch
Subject: Formatted contingency tables with (%)

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.

S?ren