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
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 | -------------|-----------|-----------|-----------| -- David Winsemius, MD Heritage Laboratories West Hartford, CT
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.In addition to my other thought: > library(xtable) > xtable(tbl) % latex table generated in R 2.10.0 by xtable 1.5-5 package % Tue Nov 10 15:44:14 2009 \begin{table}[ht] \begin{center} \begin{tabular}{rrr} \hline & X & Y \\ \hline a & 6 & 10 \\ b & 12 & 4 \\ c & 3 & 5 \\ \hline \end{tabular} \end{center} \end{table}> > S?ren > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD Heritage Laboratories West Hartford, CT