[R] SUM,COUNT,AVG

Karl Ove Hufthammer Karl.Hufthammer at math.uib.no
Tue Apr 7 16:48:39 CEST 2009


Jun Shen:

> I mainly have data frames. summarize() does not seem to work with multiple
> columns from a data frame. Any suggestion? Thanks.

summarize() *does* work with multiple columns from data frames, but the 
function you use must be able to do column-wise calculations. Example:

with(iris, summarize(iris[1:4], Species, colSums, stat.name=NULL))

If there weren’t a ‘colSums’ function, you could easily accomplish the same 
thing by using apply on the ’sum’ function:

with(iris, summarize(iris[1:4], Species, function(x) apply(x,2,sum), 
stat.name=NULL))

-- 
Karl Ove Hufthammer



More information about the R-help mailing list