How I can create a new ties.method with the R rank() function? [duplicate]

混江龙づ霸主 提交于 2019-12-04 06:09:37
mbq

I believe there is no option to do it with 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)));
}
Gregory Demin

More simple way:

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!