Hello all, I have a simple function which calculates summary statistics of a dataset in terms of a factor (say area).> x = data.frame(Area = c(rep("cleanup", 5), rep("ref", 5)), TcCB c(rnorm(5)+2, rnorm(5)));xArea TcCB 1 cleanup 2.5829747 2 cleanup 2.6796868 3 cleanup 2.5437094 4 cleanup 2.8453616 5 cleanup 1.1789683 6 ref 1.0140391 7 ref -0.8433729 8 ref 0.6512422 9 ref 0.2341083 10 ref -0.2688026> > summarystat<-function(x)+ { + no.samples<-by(x,x$Area,function(x) length(x$TcCB)) + mean<-by(x,x$Area,function(x) mean(x$TcCB)) + quantile<-by(x,x$Area,function(x) summary(x$TcCB)) + stdev<-by(x,x$Area,function(x) sd(x$TcCB)) + final<-do.call("cbind",c(quantile,mean,stdev,no.samples)) + return(final) + }> > test<-summarystat(x);testcleanup ref cleanup ref cleanup ref cleanup ref Min. 1.179 -0.8434 2.36614 0.1574428 0.6737748 0.736001 5 5 1st Qu. 2.544 -0.2688 2.36614 0.1574428 0.6737748 0.736001 5 5 Median 2.583 0.2341 2.36614 0.1574428 0.6737748 0.736001 5 5 Mean 2.366 0.1574 2.36614 0.1574428 0.6737748 0.736001 5 5 3rd Qu. 2.680 0.6512 2.36614 0.1574428 0.6737748 0.736001 5 5 Max. 2.845 1.0140 2.36614 0.1574428 0.6737748 0.736001 5 5>Now the results are arranged as per quantile function. When the results are printed, I would like to have the results for mean, stdev, no.samples, quantiles one after the other with the function names for the two factors namely cleanup and reference. Can somebody help in doing so? I did refer to a previous thread on formatting results from function. But my case is little different. Moreover, i would like to know, if there is any R command which produces this general summary statistics as above? Thanks for your time. Mathangi
Is this what you want?> tapply(x$TcCB, x$Area, my.func)$cleanup Count Mean SD Min Median 90% 95% Max Sum 5.0000000 2.1292699 0.9610394 1.1643714 2.1836433 3.0889716 3.3421262 3.5952808 10.6463495 $ref Count Mean SD Min Median 90% 95% Max Sum 5.0000000 0.1351357 0.6688342 -0.8204684 0.4874291 0.6733074 0.7058160 0.7383247 0.6756783> do.call('rbind',tapply(x$TcCB, x$Area, my.func))Count Mean SD Min Median 90% 95% Max Sum cleanup 5 2.1292699 0.9610394 1.1643714 2.1836433 3.0889716 3.342126 3.5952808 10.6463495 ref 5 0.1351357 0.6688342 -0.8204684 0.4874291 0.6733074 0.705816 0.7383247 0.6756783>You can change the following function to create the quantiles that you want: my.func <- function(a) { if (storage.mode(a) == 'integer') storage.mode(a) <- 'double' # force to 'real' if 'integer' data <- c(length(a), mean(a), sqrt(var(a)), quantile(a, c(0., 0.5, 0.9, 0.95, 1.)), sum(a)) names(data) <- c("Count", "Mean", "SD", , "Min", "Median", "90%", "95%", "Max", "Sum") data } On 2/20/06, matgopa1@umbc.edu <matgopa1@umbc.edu> wrote:> > Hello all, > > I have a simple function which calculates summary statistics of a dataset > in terms of a factor (say area). > > > x = data.frame(Area = c(rep("cleanup", 5), rep("ref", 5)), TcCB > c(rnorm(5)+2, rnorm(5)));x > Area TcCB > 1 cleanup 2.5829747 > 2 cleanup 2.6796868 > 3 cleanup 2.5437094 > 4 cleanup 2.8453616 > 5 cleanup 1.1789683 > 6 ref 1.0140391 > 7 ref -0.8433729 > 8 ref 0.6512422 > 9 ref 0.2341083 > 10 ref -0.2688026 > > > > summarystat<-function(x) > + { > + no.samples<-by(x,x$Area,function(x) length(x$TcCB)) > + mean<-by(x,x$Area,function(x) mean(x$TcCB)) > + quantile<-by(x,x$Area,function(x) summary(x$TcCB)) > + stdev<-by(x,x$Area,function(x) sd(x$TcCB)) > + final<-do.call("cbind",c(quantile,mean,stdev,no.samples)) > + return(final) > + } > > > > test<-summarystat(x);test > > cleanup ref cleanup ref cleanup ref cleanup ref > Min. 1.179 -0.8434 2.36614 0.1574428 0.6737748 0.736001 5 5 > 1st Qu. 2.544 -0.2688 2.36614 0.1574428 0.6737748 0.736001 5 5 > Median 2.583 0.2341 2.36614 0.1574428 0.6737748 0.736001 5 5 > Mean 2.366 0.1574 2.36614 0.1574428 0.6737748 0.736001 5 5 > 3rd Qu. 2.680 0.6512 2.36614 0.1574428 0.6737748 0.736001 5 5 > Max. 2.845 1.0140 2.36614 0.1574428 0.6737748 0.736001 5 5 > > > > Now the results are arranged as per quantile function. > > When the results are printed, I would like to have the results for mean, > stdev, no.samples, quantiles one after the other with the function names > for the two factors namely cleanup and reference. Can somebody help in > doing so? I did refer to a previous thread on formatting results from > function. But my case is little different. > > Moreover, i would like to know, if there is any R command which produces > this general summary statistics as above? > > Thanks for your time. > Mathangi > > ______________________________________________ > R-help@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 >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What the problem you are trying to solve? [[alternative HTML version deleted]]
Not sure I followed what it is you want but perhaps this will help: http://finzi.psych.upenn.edu/R/Rhelp02a/archive/67519.html On 2/20/06, matgopa1 at umbc.edu <matgopa1 at umbc.edu> wrote:> Hello all, > > I have a simple function which calculates summary statistics of a dataset > in terms of a factor (say area). > > > x = data.frame(Area = c(rep("cleanup", 5), rep("ref", 5)), TcCB > c(rnorm(5)+2, rnorm(5)));x > Area TcCB > 1 cleanup 2.5829747 > 2 cleanup 2.6796868 > 3 cleanup 2.5437094 > 4 cleanup 2.8453616 > 5 cleanup 1.1789683 > 6 ref 1.0140391 > 7 ref -0.8433729 > 8 ref 0.6512422 > 9 ref 0.2341083 > 10 ref -0.2688026 > > > > summarystat<-function(x) > + { > + no.samples<-by(x,x$Area,function(x) length(x$TcCB)) > + mean<-by(x,x$Area,function(x) mean(x$TcCB)) > + quantile<-by(x,x$Area,function(x) summary(x$TcCB)) > + stdev<-by(x,x$Area,function(x) sd(x$TcCB)) > + final<-do.call("cbind",c(quantile,mean,stdev,no.samples)) > + return(final) > + } > > > > test<-summarystat(x);test > > cleanup ref cleanup ref cleanup ref cleanup ref > Min. 1.179 -0.8434 2.36614 0.1574428 0.6737748 0.736001 5 5 > 1st Qu. 2.544 -0.2688 2.36614 0.1574428 0.6737748 0.736001 5 5 > Median 2.583 0.2341 2.36614 0.1574428 0.6737748 0.736001 5 5 > Mean 2.366 0.1574 2.36614 0.1574428 0.6737748 0.736001 5 5 > 3rd Qu. 2.680 0.6512 2.36614 0.1574428 0.6737748 0.736001 5 5 > Max. 2.845 1.0140 2.36614 0.1574428 0.6737748 0.736001 5 5 > > > > Now the results are arranged as per quantile function. > > When the results are printed, I would like to have the results for mean, > stdev, no.samples, quantiles one after the other with the function names > for the two factors namely cleanup and reference. Can somebody help in > doing so? I did refer to a previous thread on formatting results from > function. But my case is little different. > > Moreover, i would like to know, if there is any R command which produces > this general summary statistics as above? > > Thanks for your time. > Mathangi > > ______________________________________________ > 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 >