df_select_columns Function

public function df_select_columns(df, column_indices) result(new_df)

Select specific columns from a data frame

Creates a new data frame containing only the specified columns

@param[in] df The source data frame @param[in] column_indices Array of column indices to select @return new_df New data frame with selected columns

Arguments

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

Return Value type(data_frame)


Source Code

    function df_select_columns(df, column_indices) result(new_df)
        type(data_frame), intent(in) :: df
        integer, dimension(:), intent(in) :: column_indices
        type(data_frame) :: new_df

        integer :: i, col_idx
        type(column) :: col

        call new_df % new(df % get_max_char_len())

        do i = 1, size(column_indices)
            col_idx = column_indices(i)
            if (col_idx < 1 .or. col_idx > df % ncols()) then
                error stop "Column index out of range in df_select_columns"
            end if

            col = df % get_data_col(col_idx)

            select case (col % get_type())
            case (REAL_NUM)
                if (df % get_with_headers()) then
                    call df_append_real(new_df, col % getr(), df % header(col_idx))
                else
                    call df_append_real(new_df, col % getr())
                end if
            case (INTEGER_NUM)
                if (df % get_with_headers()) then
                    call df_append_integer(new_df, col % geti(), df % header(col_idx))
                else
                    call df_append_integer(new_df, col % geti())
                end if
            case (LOGICAL_NUM)
                if (df % get_with_headers()) then
                    call df_append_logical(new_df, col % getl(), df % header(col_idx))
                else
                    call df_append_logical(new_df, col % getl())
                end if
            case (CHARACTER_NUM)
                if (df % get_with_headers()) then
                    call df_append_character(new_df, col % getch(), df % header(col_idx))
                else
                    call df_append_character(new_df, col % getch())
                end if
            case (COMPLEX_NUM)
                if (df % get_with_headers()) then
                    call df_append_complex(new_df, col % getc(), df % header(col_idx))
                else
                    call df_append_complex(new_df, col % getc())
                end if
            end select
        end do
    end function df_select_columns