Skip to content

Rank elements

Source code

Description

Assign ranks to data, dealing with ties appropriately.

Usage

<Expr>$rank(
  method = c("average", "min", "max", "dense", "ordinal", "random"),
  descending = FALSE,
  seed = NULL
)

Arguments

method String, one of “average” (default), “min”, “max”, “dense”, “ordinal”, “random”. The method used to assign ranks to tied elements:
  • “average”: The average of the ranks that would have been assigned to all the tied values is assigned to each value.
  • “min”: The minimum of the ranks that would have been assigned to all the tied values is assigned to each value. (This is also referred to as "competition" ranking.)
  • “max” : The maximum of the ranks that would have been assigned to all the tied values is assigned to each value.
  • “dense”: Like ‘min’, but the rank of the next highest element is assigned the rank immediately after those assigned to the tied elements.
  • “ordinal” : All values are given a distinct rank, corresponding to the order that the values occur in the Series.
  • “random” : Like ‘ordinal’, but the rank for ties is not dependent on the order that the values occur in the Series.
descending Rank in descending order.
seed string parsed or number converted into uint64. Used if method="random".

Value

Expr

Examples

library("polars")

#  The 'average' method:
pl$DataFrame(a = c(3, 6, 1, 1, 6))$
  with_columns(rank = pl$col("a")$rank())
#> shape: (5, 2)
#> ┌─────┬──────┐
#> │ a   ┆ rank │
#> │ --- ┆ ---  │
#> │ f64 ┆ f64  │
#> ╞═════╪══════╡
#> │ 3.0 ┆ 3.0  │
#> │ 6.0 ┆ 4.5  │
#> │ 1.0 ┆ 1.5  │
#> │ 1.0 ┆ 1.5  │
#> │ 6.0 ┆ 4.5  │
#> └─────┴──────┘
#  The 'ordinal' method:
pl$DataFrame(a = c(3, 6, 1, 1, 6))$
  with_columns(rank = pl$col("a")$rank("ordinal"))
#> shape: (5, 2)
#> ┌─────┬──────┐
#> │ a   ┆ rank │
#> │ --- ┆ ---  │
#> │ f64 ┆ u32  │
#> ╞═════╪══════╡
#> │ 3.0 ┆ 3    │
#> │ 6.0 ┆ 4    │
#> │ 1.0 ┆ 1    │
#> │ 1.0 ┆ 2    │
#> │ 6.0 ┆ 5    │
#> └─────┴──────┘