Rank Variable(s)

Six variations on ranking functions, mimicking the ranking functions described in SQL2003. They are currently implemented using the built in rank function, and are provided mainly as a convenience when converting between R and SQL.

Ranks will be stored in new variables with the prefix or suffix specified.

All ranking functions map smallest inputs to smallest outputs.

Rank Variable(s)
Arguments
  1. x: A vector of values to rank. Missing values are left as is. If you want to treat them as the smallest or largest values, replace with Inf or -Inf before ranking.

  2. n: number of groups to split up into.

row_number()

equivalent to rank(ties.method = "first")

min_rank()

equivalent to rank(ties.method = "min")

dense_rank()

like min_rank(), but with no gaps between ranks

percent_rank()

a number between 0 and 1 computed by rescaling min_rank to [0, 1]

cume_dist()

a cumulative distribution function. Proportion of all values less than or equal to the current rank.

ntile()

a rough rank, which breaks the input vector into n buckets.

Last modified: 22 December 2025