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:
@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.