Skip to content

mean vs sum behavior

2 messages · Emmanuel Castella, Gavin Simpson

#
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
#
On Mon, 2008-03-31 at 18:41 +0200, Emmanuel Castella wrote:
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:
then
or even quicker and easier:
HTH

G