[R] formatting results from a function argument

matgopa1@umbc.edu matgopa1 at umbc.edu
Mon Feb 20 06:29:51 CET 2006


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




More information about the R-help mailing list