[R] why function 'sum' within 'aggregate' need an 'index'?

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Jul 9 14:27:39 CEST 2007


On Mon, 2007-07-09 at 20:14 +0800, jingjiang yan wrote:
> Hi, people.
> I am using R-2.5.0 now, when tried the function aggregate with sum, it
> showed an  error as following:
> > a <- gl(3,10)
> > b <- rnorm(30)
> > aggregate(b,list(a),sum)
> #  here is the error message,  it complained that an error in FUN(X[[1L]],
> missing "INDEX", and no defaults value.
> 
> but the tapply function will be okay.
> > tapply(b,list(a),sum)
>         1         2         3
>  2.113349 -5.972195  4.854731
> 
> furthermore, when I was using the R-2.5.0 pre-release version before.
> it could work well.
> > a <- gl(3,10)
> > b <- rnorm(30)
> > aggregate(b,list(a),sum)      # it works well
>   Group.1          x
> 1       1 -1.0330482
> 2       2  0.1235796
> 3       3 -1.0086930
> > tapply(b,list(a),sum)            # so does tapply
>          1          2          3
> -1.0330482  0.1235796 -1.0086930
> 
> So, who can tell what should I do to overcome this?
> thanks a lot.

Update to R 2.5.1 as your first example works for me [version info
below]. If that is not possible, just use the tapply version for now.

G

> version
               _
platform       i686-pc-linux-gnu
arch           i686
os             linux-gnu
system         i686, linux-gnu
status         Patched
major          2
minor          5.1
year           2007
month          07
day            05
svn rev        42131
language       R
version.string R version 2.5.1 Patched (2007-07-05 r42131)

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list