Rank based on several variables

前端 未结 2 1807
日久生厌
日久生厌 2021-01-19 00:15

This is a small example. In my larger dataset, I have multiple years of data and the number of observations per group (div) are not always equal.

Example data:

2条回答
  •  梦谈多话
    2021-01-19 00:58

    @eddi's answer is already very nice. I just wanted to illustrate the same using frank() function from the development version of data.table, v1.9.5, which can compute ranks on vectors, lists, data.frames or data.tables.

    # from @eddi's
    setDT(df)[, div.clean := sub('(\\d+).*', '\\1', div)]
    
    df[, position := frank(.SD, -pts, -x, ties.method="first"), by=div]
    df[, final := frank(.SD, div.clean, position, ties.method="average")]
    

    This also retains the original order, if that's of any importance.

    I'll leave the conversion to dplyr to you.

提交回复
热议问题