[R] mean etc at the end of column

Douglas Bates bates at stat.wisc.edu
Fri Apr 11 14:35:49 CEST 2003


Martin Wegmann <wegmann at biozentrum.uni-wuerzburg.de> writes:

> Hello
> 
> I would like to know how I can add mean, max etc (summary(), acf or lm()
> ...)) at the end of a column in a data frame or as a new data frame with
> several columns corresponding to the input columns instead of having a
> separate output.

use rbind to add a row created by
 unlist(lapply(dataframe, summary.function))
as in

> data(women)
> wsumry = rbind(women, unlist(lapply(women, mean)))
> wsumry
   height   weight
1      58 115.0000
2      59 117.0000
3      60 120.0000
4      61 123.0000
5      62 126.0000
6      63 129.0000
7      64 132.0000
8      65 135.0000
9      66 139.0000
10     67 142.0000
11     68 146.0000
12     69 150.0000
13     70 154.0000
14     71 159.0000
15     72 164.0000
16     65 136.7333

However, you may want to ask yourself if you really want to do this.
It is hard to distinguish that last row of means from the rest of the
data.  In general I would suggest keeping the summary separate from
the original data.

-- 
Douglas Bates                            bates at stat.wisc.edu
Statistics Department                    608/262-2598
University of Wisconsin - Madison        http://www.stat.wisc.edu/~bates/



More information about the R-help mailing list