Message-ID: <f8e6ff050809261454t1f6ea29bycde43b6d27a03a44@mail.gmail.com>
Date: 2008-09-26T21:54:10Z
From: Hadley Wickham
Subject: Newbie: Ranking a data frame, grouped by 2 or more columns
In-Reply-To: <82ba77b80809261354q47c21a64rb6ee185e28e6339a@mail.gmail.com>
On Fri, Sep 26, 2008 at 3:54 PM, Matthew Pettis
<matthew.pettis at gmail.com> wrote:
> Hi,
>
> I'd like to rank obs in a data frame as subset by 2 or more columns...
> The example input would look like the following:
>
> ====+====+====+====+
> x y v
> -- -- --
> a w 200
> a w 100
> b w 500
> b w 200
> b z 300
> b z 400
> ====+====+====+====+
>
> And the data frame I want to create is below:
> ====+====+====+====+
> x y v rank
> -- -- -- ----
> a w 200 1
> a w 100 2
> b w 500 1
> b w 200 2
> b z 300 2
> b z 400 1
> ====+====+====+====+
>
> Can someone help me with this?
This is easy to do with the (very soon to be released) plyr package:
library(plyr)
ddply(df, .(x, y), transform, rank = rank(v))
You can learn more about it at http://had.co.nz/plyr
Hadley
--
http://had.co.nz/