[R] question on aggregate

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jan 11 04:13:01 CET 2011


On Mon, Jan 10, 2011 at 8:23 PM, analyst41 at hotmail.com
<analyst41 at hotmail.com> wrote:
> an example available on the net goes like
>
>> df
>  identifier quantity
> 1          1       10
> 2          1       20
> 3          2       30
> 4          1       15
> 5          2       10
> 6          3       20
>> aggregate(df$quantity, by=list(df$identifier), sum)
>  Group.1  x
> 1       1 45
> 2       2 40
> 3       3 20
>
>
> I'd like Group.1 to retain the name "identifier" and would like to
> control what "x" get called in the output.  Thanks.

Try these:

> aggregate(quantity ~ identifier, df, sum)
  identifier quantity
1          1       45
2          2       40
3          3       20
>
> aggregate(df["quantity"], df["identifier"], sum)
  identifier quantity
1          1       45
2          2       40
3          3       20
>
> aggregate(list(Quantity = df$quantity), df["identifier"], sum)
  identifier Quantity
1          1       45
2          2       40
3          3       20

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list