views:

82

answers:

3

Hello guys, I'm trying to order this dataframe by population and date, so i'm using order() and rank() function :


   idgeoville date    population
1  5          1950     500        
2  8          1950     450        
3  4          1950     350        
4  3          1950     350        
5  4          2000     650        
6  5          2000     500        
7  8          2000     500        
8  8          2000     450        

With ties.method = "first" i have no problem, finally i'm producing this dataframe :


   idgeoville date    population  rank
1  5          1950     500        1
2  8          1950     450        2
3  4          1950     350        3
4  3          1950     350        4
5  4          2000     650        1 
6  5          2000     500        2
7  8          2000     500        3
8  8          2000     450        4

But in fact, i want a dataframe with equal ranking for equal population rank, like this :


   idgeoville date    population  rank
1  5          1950     500        1
2  8          1950     450        2
3  4          1950     350        3
4  3          1950     350        3
5  4          2000     650        1 
6  5          2000     500        2
7  8          2000     500        2
8  8          2000     450        3

How can i resolve this problem with R ? With a custom ties.method() or another R tricks ?

Thanks in advance for your help :)

+3  A: 

I believe there is no option to do it with rank; here is a custom function that will do what you want, but may be too slow if your data is huge:

Rank<-function(d){
j<-unique(rev(sort(d)));
return(sapply(d,function(dd) which(dd==j)));
}
mbq
Thx a lot, it's ok ! But if another person have better and/or faster solution with R package, i take !
reyman64
A: 

This answers a slightly different question, namely how to sort a data.frame object based on multiple columns. To do this, you could use the function sort_df in package reshape:

> library(reshape)
> sort_df(df,vars=c('date','population'))
  idgeoville date population
3          4 1950        350
4          3 1950        350
2          8 1950        450
1          5 1950        500
8          8 2000        450
6          5 2000        500
7          8 2000        500
5          4 2000        650
nullglob
+1  A: 

More simple way:

pop.rank<-as.numeric(factor(population))

Cekory