Reference · Statistics

stats.rank

signature
stats.rank(list | null) -> list

What it does

The rank of each number in a list, counting from 1 for the smallest, as a list in the same order as the data. Values that tie share the average of the ranks they would have taken, which can be a float such as 2.5; every other rank is an int. These are the ranks stats.correlation(x, y, "ranked") uses. It is not in Python's statistics module.

Examples

print(stats.rank([10, 30, 20]))print(stats.rank([10, 20, 20, 30]))print(stats.rank([72, 85, 90, 66, 85, 78]))
output
[1, 3, 2][1, 2.5, 2.5, 4][2, 4.5, 6, 1, 4.5, 3]

Edge cases

print(stats.rank([5, 5, 5]), stats.rank([1, 1, 1, 1, 2]), stats.rank([2, 2, 1]))print(stats.rank([]), stats.rank(null), stats.rank([10, 10.0, 5]))print(stats.rank([10, 30, 20] | map((x) => -x)))
output
[2, 2, 2] [2.5, 2.5, 2.5, 2.5, 5] [2.5, 2.5, 1][] [] [2.5, 2.5, 1][3, 1, 2]

Errors

An item that is not a number is a runtime error, not an error value. So is an argument that is not a list or null.

this one fails
print(stats.rank(["a", "b"]))
output
line 1: `stats.rank` expects a list of numbers, but item 0 is str

Chaining

print([4, 1, 3] | stats.rank | map((r) => r * 10))print([3, 1, 2] | stats.rank | map((r) => r == 1))print([1, 2, 3] | stats.rank | sum)
output
[30, 10, 20][false, true, false]6

See also

stats.correlation, stats.percentile, vec.argsort, sort

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On stats.rank(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo