df_median_integer Function

public function df_median_integer(df, col_index) result(med)

Calculate median of integer column

Arguments

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

Return Value real(kind=rk)


Source Code

    function df_median_integer(df, col_index) result(med)
        type(data_frame), intent(in) :: df
        integer, intent(in) :: col_index
        real(rk) :: med

        integer(ik), dimension(:), allocatable :: col, sorted_col
        integer :: n, mid
        type(column) :: data_col

        if (col_index < 1 .or. col_index > df % ncols()) error stop "column index out of range"

        data_col = df % get_data_col(col_index)
        if (data_col % get_type() /= INTEGER_NUM) error stop "column is not integer type"

        col = data_col % geti()
        n = size(col)
        sorted_col = col

        call quick_sort_integer(sorted_col, 1, n)

        if (mod(n, 2) == 1) then
            mid = (n + 1) / 2
            med = real(sorted_col(mid), rk)
        else
            mid = n / 2
            med = (real(sorted_col(mid), rk) + real(sorted_col(mid + 1), rk)) / 2.0_rk
        end if
    end function df_median_integer