From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>

Date: Mon, 31 Mar 2008 18:02:03 +0100

Date: Mon, 31 Mar 2008 18:02:03 +0100

On Mon, 2008-03-31 at 18:41 +0200, Emmanuel Castella wrote:

> Dear all

*> Could someone explain me why
**> lapply(split(x,fac),mean)
**> returns means per levels of fac for each column of x
**> whereas
**> lapply(split(x,fac),sum)
**> does not return sums per level of fac and columns of x, but adds all
**> columns together?
**> Hence, how can I get sum to behave as mean in this instance?
**> Thank you very much in advance
**> E. Castella
*

You didn't tell us what x is, but I suspect a data.frame. mean has a method for class "data.frame", which returns the mean of each column. Sum doesn't have any methods and hence works by summing all the numbers.

If you want to replicate the mean behaviour with sum, the following would suffice:

*> fac <- gl(4, 50)
*

> dat <- data.frame(a = rnorm(200), b = rnorm(200), c = rnorm(200))

> sp <- split(dat, fac)

then

> lapply(sp, function(x) sapply(x, sum))

or even quicker and easier:

> lapply(sp, colSums)

**HTH
**
G

-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ 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.Received on Mon 31 Mar 2008 - 17:09:18 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 31 Mar 2008 - 18:30:27 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.
*