Matthew Pettis
2008-Sep-26 20:54 UTC
[R] Newbie: Ranking a data frame, grouped by 2 or more columns
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? Thanks, Matt -- It is from the wellspring of our despair and the places that we are broken that we come to repair the world. -- Murray Waas
Henrique Dallazuanna
2008-Sep-26 21:43 UTC
[R] Newbie: Ranking a data frame, grouped by 2 or more columns
Try this: DF V1 V2 V3 1 a w 200 2 a w 100 3 b w 500 4 b w 200 5 b z 300 6 b z 400 DF$rank <- unlist(lapply(split(DF$V3, list(DF$V1, DF$V2), drop = T), rank)) On Fri, Sep 26, 2008 at 5: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? > > Thanks, > Matt > -- > It is from the wellspring of our despair and the places that we are > broken that we come to repair the world. > -- Murray Waas > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
hadley wickham
2008-Sep-26 21:54 UTC
[R] Newbie: Ranking a data frame, grouped by 2 or more columns
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/
jim holtman
2008-Sep-26 23:28 UTC
[R] Newbie: Ranking a data frame, grouped by 2 or more columns
Try this:> xV1 V2 V3 1 a w 200 2 a w 100 3 b w 500 4 b w 200 5 b z 300 6 b z 400> x$rank <- ave(x$V3, x$V1, x$V2, FUN=rank) > xV1 V2 V3 rank 1 a w 200 2 2 a w 100 1 3 b w 500 2 4 b w 200 1 5 b z 300 1 6 b z 400 2>On Fri, Sep 26, 2008 at 4: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? > > Thanks, > Matt > -- > It is from the wellspring of our despair and the places that we are > broken that we come to repair the world. > -- Murray Waas > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?