avg_rank

The avg_rank operator lets you rank elements of an array.

Synopsis

avg_rank( array [, attribute] [, dimension_1, dimension_2,...]] )

Summary

The avg_rank operator ranks array elements and calculates average rank as the average of the upper bound (UB) and lower bound (LB) rankings. The LB ranking of A, same as returned by rank, is the number of elements less than A, plus 1. The UB ranking of A is the number of elements less than or equal to A, plus 1. avg_rank returns the average of the UB and LB ranking for each element.

When no duplicates are present, each element the UB rank is the same as the LB rank and avg_rank returns exactly the same result as rank.

Example

To calculate ranks along the columns of an array where there are ties within columns, do the following:

  1. To create a 4×4 array called rank, enter the following:

    AFL% create array rank_array <val:double>[i=0:3; j=0:3];
  2. To put random values of 0–6 into the rank array, enter:

    AFL% store(build(rank_array,random()%7/1.0),rank_array); 


    The random function in the build operator above yields different answers each time, but the output might look like this: 

    {i,j} val
    {0,0} 4
    {0,1} 4
    {0,2} 2
    {0,3} 4
    {1,0} 0
    {1,1} 2
    {1,2} 2
    {1,3} 2
    {2,0} 2
    {2,1} 4
    {2,2} 1
    {2,3} 4
    {3,0} 4
    {3,1} 6
    {3,2} 3
    {3,3} 0

     

  3. Rank the val attribute valus in rank_array by dimension i:

    AFL% avg_rank(rank_array,val,i); 


    The output is:

    {i,j} val,val_rank
    {0,0} 4,3
    {0,1} 4,3
    {0,2} 2,1
    {0,3} 4,3
    {1,0} 0,1
    {1,1} 2,3
    {1,2} 2,3
    {1,3} 2,3
    {2,0} 2,2
    {2,1} 4,3.5
    {2,2} 1,1
    {2,3} 4,3.5
    {3,0} 4,3
    {3,1} 6,4
    {3,2} 3,2
    {3,3} 0,1

 

Â