On Fri, Jun 1, 2012 at 11:23 AM, Brigid Mooney wrote:
> Hopefully this is an easy problem...
>
> I'm trying to add a partitioned rank column to a data frame where the
> rank is calculated separately across a partition by categories, the
> way you could easily do in SQL. I found this solution in t
4 PM
Subject: Re: [R] Add rank column to data frame as in SQL...
Sorry, I meant to put that it didn't work for "GM", not "Toyota".
> DF[DF$Company=="GM",]
Company Person Salary rank
1 GM A 7067.905 4
3 GM C 31627.171 8
Sorry, I meant to put that it didn't work for "GM", not "Toyota".
> DF[DF$Company=="GM",]
Company PersonSalary rank
1 GM A 7067.9054
3 GM C 31627.1718
5 GM E 66200.5087
7 GM G 91287.5923
10 GM J 33239.4676
13 GM
Try using the rank function instead of the order function.
rank(x) is order(order(x)) if there are no ties.
Since you want reverse ranks do either rank(-x) or length(x)+1-rank(x).
E.g.,
> DF <- within(DF, rank2 <- ave(Salary, Company, FUN=function(x)rank(-x)))
> DF[DF$Company=="Toyota",]
C
Hi,
Couldn't find any problems if I understand what you mean.
A.K.
> DF <-within(DF,rank<-ave(Salary,Company,FUN=function(x)rev(order(x
> DF
Company Person Salary rank
1 GM A 7067.905 4
2 Ford B 9946.616 3
3 GM C 31627.171 8
4 Toyota D
5 matches
Mail list logo