I\'m trying to compute a medal table for a sports event.
My data looks like this:
test <- data.frame(\"ID\" = c(\"1_1\", \"1_2\", \"1_3\", \"1_4\
You may use the data.table
equivalent of base::rank
, frank
. A nice feature with frank
is that it accepts, not only vectors (as in rank
), but also a data.frame
or a data.table
as input. For these types of objects, the rank may be based on several columns.
Using your original data.frame
:
test$rank <- data.table::frank(test, -gold, -silver, -bronze, ties.method = "min")
Or if you want to go all in with data.table
functions:
setDT(test)[ , rank := frank(test, -gold, -silver, -bronze, ties.method = "min")]
setorder(test, rank)