df_median_real Function

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

Calculate median of real 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_real(df, col_index) result(med)
        type(data_frame), intent(in) :: df
        integer, intent(in) :: col_index
        real(rk) :: med

        real(rk), 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() /= REAL_NUM) error stop "column is not real type"

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

        ! Simple bubble sort for median calculation
        call quick_sort_real(sorted_col, 1, n)

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