R users, I intention is to calculate some summary statistics across factor levels. I know that in Hmisc package there is a summary function which produces neat summary statistics when using "cross" option. I would like to produce similar output with N and Missing columns but produce a data.frame. Is there any built-in function for that? #example data install.packages("Hmisc") library(Hmisc) sek <- seq(1, nrow(Indometh), 9) Indometh$time[sek] <- NA Indometh$timeclass <- factor(cut(Indometh$time, breaks=c(0,2,4,6,8,10))) Indometh with(Indometh, summary(conc ~ Subject + timeclass, method="cross")) #similar with aggregate and reshape but no N or Missing count i.mean <- aggregate(Indometh$conc, list(Indometh$Subject, Indometh$timeclass), mean, na.rm=T) i.mean.rhsp <- reshape(i.mean, v.names="x", idvar="Group.1", timevar="Group.2", direction="wide") i.mean.rhsp # N and missing columns needed Thanks, Lauri
Lauri Nikkinen wrote:> R users, > > I intention is to calculate some summary statistics across factor > levels. I know that in Hmisc package there is a summary function which > produces neat summary statistics when using "cross" option. I would > like to produce similar output with N and Missing columns but produce > a data.frame. Is there any built-in function for that?Take a look at the Hmisc summarize function.> > #example data > install.packages("Hmisc") > library(Hmisc) > sek <- seq(1, nrow(Indometh), 9) > Indometh$time[sek] <- NA > Indometh$timeclass <- factor(cut(Indometh$time, breaks=c(0,2,4,6,8,10))) > Indometh > with(Indometh, summary(conc ~ Subject + timeclass, method="cross"))If using summary I suggest summary(conc ~ Subject + cut2(time, c(0,2,4,6,8,10)), data=Indometh, method='cross') Frank> > #similar with aggregate and reshape but no N or Missing count > i.mean <- aggregate(Indometh$conc, list(Indometh$Subject, > Indometh$timeclass), mean, na.rm=T) > i.mean.rhsp <- reshape(i.mean, v.names="x", idvar="Group.1", > timevar="Group.2", direction="wide") > i.mean.rhsp # N and missing columns needed > > Thanks, > Lauri > > ______________________________________________ > 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. >-- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
Try this: sek <- seq(1, nrow(Indometh), 9) Indometh$time[sek] <- NA Indometh$timeclass <- factor(cut(Indometh$time, breaks=c(0,2,4,6,8,10))) x <- summary(conc ~ Subject + timeclass, method="cross", data=Indometh) vec <- x$S dim(vec) <- attr(x, "out.attrs")$dim dimnames(vec) <- attr(x, "out.attrs")$dimnames as.data.frame(vec) On Wed, Apr 30, 2008 at 7:27 AM, Lauri Nikkinen <lauri.nikkinen@iki.fi> wrote:> R users, > > I intention is to calculate some summary statistics across factor > levels. I know that in Hmisc package there is a summary function which > produces neat summary statistics when using "cross" option. I would > like to produce similar output with N and Missing columns but produce > a data.frame. Is there any built-in function for that? > > #example data > install.packages("Hmisc") > library(Hmisc) > sek <- seq(1, nrow(Indometh), 9) > Indometh$time[sek] <- NA > Indometh$timeclass <- factor(cut(Indometh$time, breaks=c(0,2,4,6,8,10))) > Indometh > with(Indometh, summary(conc ~ Subject + timeclass, method="cross")) > > #similar with aggregate and reshape but no N or Missing count > i.mean <- aggregate(Indometh$conc, list(Indometh$Subject, > Indometh$timeclass), mean, na.rm=T) > i.mean.rhsp <- reshape(i.mean, v.names="x", idvar="Group.1", > timevar="Group.2", direction="wide") > i.mean.rhsp # N and missing columns needed > > Thanks, > Lauri > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]