Add a “rank” column to a data frame

前端 未结 5 1973
滥情空心
滥情空心 2020-11-27 14:43

I have a dataframe with counts of different items, in different years:

df <- data.frame(item = rep(c(\'a\',\'b\',\'c\'), 3),
                 year = rep(c         


        
5条回答
  •  半阙折子戏
    2020-11-27 15:05

    While using the answers given by others, I found that the following performs faster than the transform and dyplr variants:

    df$year.rank <- ave(count, year, FUN = function(x) rank(-x, ties.method = "first"))
    

提交回复
热议问题