Re: [R] mean vs sum behavior

From: Gavin Simpson <>
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)


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

or even quicker and easier:

> lapply(sp, colSums)


 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e]
 Gower Street, London          [w]
 UK. WC1E 6BT.                 [w]

______________________________________________ mailing list
PLEASE do read the posting guide
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 Please read the posting guide before posting to the list.

list of date sections of archive