[R] A file manipulation question

Marc Schwartz MSchwartz at medanalytics.com
Thu Mar 4 14:42:54 CET 2004


<Banging head on desk>

Yep. Knew that. There was even an exchange on r-help last fall on this
and I was one of the participants, though with a slightly different
solution using as.data.frame, which gets us to the same end result:

> with(df, aggregate(as.data.frame(Contract), list(ID = ID), max))
  ID Contract
1 01        1
2 02        3
3 03        2

Substituting list() for as.data.frame() works of course in a similar
fashion, which Simon Fear pointed out in that same thread:

> with(df, aggregate(list(Contract = Contract), list(ID = ID), max))
  ID Contract
1 01        1
2 02        3
3 03        2

and of course you can rename the Contract column directly if you wish to
give the name further meaning:

> with(df, aggregate(list(Contract.Max = Contract), list(ID = ID), max))
  ID Contract.Max
1 01            1
2 02            3
3 03            2

The key is to not have the 'x' argument ('Contract' in this case) passed
as a vector.

Thanks Gabor!


On Wed, 2004-03-03 at 23:12, Gabor Grothendieck wrote:
> You can ensure the name gets set appropriately like this:
> 
> > aggregate(list(Contract=df$Contract), list(ID=df$ID), max)
>   ID Contract
> 1 01        1
> 2 02        3
> 3 03        2
> 
> 
> ---
> Date:   Wed, 03 Mar 2004 21:46:27 -0600 
> From:   Marc Schwartz <MSchwartz at medanalytics.com>
> To:   Greg Blevins <gblevins at mn.rr.com> 
> Cc:   R-Help <r-help at stat.math.ethz.ch> 
> Subject:   Re: [R] A file manipulation question 
> 
> [...]
> 
> > aggregate(df$Contract, list(ID = df$ID), max)
> ID x
> 1 01 1
> 2 02 3
> 3 03 2
> 
> 
> See ?aggregate for more information. By default, aggregate() names the
> function derived column as 'x'. You can of course rename it as you need.
> 
> HTH,
> 
> Marc Schwartz
> 
> 
> 
> _______________________________________________
> No banners. No pop-ups. No kidding.
> Introducing My Way - http://www.myway.com




More information about the R-help mailing list