How can I create a new link method with the R rank () function?

I am trying to sort this data file by quantity and date, so I use the order() and rank() functions:

 > df <- data.frame(idgeoville = c(5, 8, 4, 3, 4, 5, 8, 8), date = c(rep(1950, 4), rep(2000, 4)), population = c(500, 450, 350, 350, 650, 500, 500, 450)) > df 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 create this data file:

  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 actually, I want a framework with an equal rank for an equal population rank , for example:

  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 solve this problem with R? With custom ties.method() or other R tricks?

+7
sorting select r dataframe ranking
source share
3 answers

I believe there is no chance of doing this with a rank; here is a custom function that will do what you want, but it 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))); } 
+4
source share

A simpler way:

 pop.rank <- as.numeric(factor(population)) 
+6
source share

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

 > 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 
+1
source share

All Articles