From: Keith Wong <keithw_at_med.usyd.edu.au>

Date: Mon, 14 May 2007 11:56:45 +1000

+-------+--------------+--------------+

|age |46.5/49.9/53.2|46.7/50.0/53.4|

+-------+--------------+--------------+

|sex : m| 47% (123) | 44% (104) |

+-------+--------------+--------------+

* >
*

* >
*

* > # latex(x) generates a very nicely formatted table
*

* > # but I'd like "mean (standard deviation)" instead of quartiles.
*

+-------+--------------+--------------+

|age |46.5/49.9/53.2|46.7/50.0/53.4|

+-------+--------------+--------------+

|sex : m| 47% (123) | 44% (104) |

+-------+--------------+--------------+

* >
*

* >
*

* > (x1 = summarize(cbind(age), llist(treatment), FUN = g,
*

stat.name=c("n", "mean", "sd")))

Date: Mon, 14 May 2007 11:56:45 +1000

I illustrate my question below using an example from the book.

* > ####
** > library(Hmisc)
** >
** > set.seed(173)
** > sex = factor(sample(c("m", "f"), 500, rep = T))
** > age = rnorm(500, 50, 5)
** > treatment = factor(sample(c("Drug", "Placebo"), 500, rep = T))
** > summary(sex ~ treatment, fun = table)
*

sex N=500

Descriptive Statistics by treatment

+-------+--------------+--------------+

| |Drug |Placebo | | |(N=263) |(N=237) |

+-------+--------------+--------------+

|age |46.5/49.9/53.2|46.7/50.0/53.4|

+-------+--------------+--------------+

|sex : m| 47% (123) | 44% (104) |

+-------+--------------+--------------+

* > # this function from
*

http://tolstoy.newcastle.edu.au/R/e2/help/06/11/4713.html

> g <- function(y) {

+ s <- apply(y, 2, + function(z) { + z <- z[!is.na(z)] + n <- length(z) + if(n==0) c(NA,NA,NA,0) else + if(n==1) c(z, NA,NA,1) else { + m <- mean(z) + s <- sd(z) + c(N=n, Mean=m, SD=s) + } + }) + w <- as.vector(s) + names(w) <- as.vector(outer(rownames(s), colnames(s), paste, sep='')) + w + }

* >
** > summary(treatment ~ age + sex, method = "reverse", fun = g)
** > # does not work, 'fun' or 'FUN" argument is ignored.
*

Descriptive Statistics by treatment

+-------+--------------+--------------+

| |Drug |Placebo | | |(N=263) |(N=237) |

+-------+--------------+--------------+

|age |46.5/49.9/53.2|46.7/50.0/53.4|

+-------+--------------+--------------+

|sex : m| 47% (123) | 44% (104) |

+-------+--------------+--------------+

stat.name=c("n", "mean", "sd")))

treatment n mean sd

1 Drug 263 49.9 4.94

2 Placebo 237 50.1 4.97

* >
** > # this works but table is rotated, and it count data has to be
** > # treated separately.
*

-- Keith Wong PhD candidate Sleep & Circadian Research Group Woolcock Institute of Medical Research email keithw_at_med.usyd.edu.au Phone +61 2 9515 8981 Fax +61 2 9515 7070 Mail PO Box M77, Missenden Road NSW 2050, Australia ______________________________________________ R-help_at_stat.math.ethz.ch 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.Received on Mon 14 May 2007 - 02:04:20 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Mon 14 May 2007 - 05:31:36 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*