df_rank_integer Function

public function df_rank_integer(df, col_index, ascending) result(ranks)

Rank values in an integer column

Assigns rank positions to each value in the column (1 = smallest/largest)

@param[in] df The data frame instance @param[in] col_index Index of column to rank @param[in] ascending Optional ranking direction (default: .true. for ascending) @return Array of integer ranks corresponding to each row

Arguments

Type IntentOptional Attributes Name
type(data_frame), intent(in) :: df
integer, intent(in) :: col_index
logical, intent(in), optional :: ascending

Return Value integer(kind=ik), dimension(:), allocatable


Source Code

    function df_rank_integer(df, col_index, ascending) result(ranks)
        type(data_frame), intent(in) :: df
        integer, intent(in) :: col_index
        logical, intent(in), optional :: ascending
        integer(ik), dimension(:), allocatable :: ranks

        integer(ik), dimension(:), allocatable :: col, sorted_col
        type(column) :: data_col
        integer :: i, j, n
        logical :: asc

        asc = .true.
        if (present(ascending)) asc = ascending

        data_col = df % get_data_col(col_index)
        col = data_col % geti()
        n = size(col)
        allocate (ranks(n))
        allocate (sorted_col(n))
        sorted_col = col

        call quick_sort_integer(sorted_col, 1, n)

        if (.not. asc) then
            sorted_col = sorted_col(n:1:-1)
        end if

        do i = 1, n
            do j = 1, n
                if (col(i) == sorted_col(j)) then
                    ranks(i) = j
                    exit
                end if
            end do
        end do

        deallocate (sorted_col)
    end function df_rank_integer