df_insert_column_real Subroutine

public subroutine df_insert_column_real(df, data, position, header)

Insert a real column at a specific position

@param df The data frame to modify @param data The data to insert @param position Position to insert (1 = first column) @param header Optional column header

Arguments

Type IntentOptional Attributes Name
type(data_frame), intent(inout) :: df
real(kind=rk), intent(in), dimension(:) :: data
integer, intent(in) :: position
character(len=*), intent(in), optional :: header

Source Code

    subroutine df_insert_column_real(df, data, position, header)
        type(data_frame), intent(inout) :: df
        real(rk), dimension(:), intent(in) :: data
        integer, intent(in) :: position
        character(len=*), intent(in), optional :: header

        type(data_frame) :: temp_df
        integer :: i, original_cols

        if (position < 1 .or. position > df % ncols() + 1) error stop "invalid insertion position"
        if (df % nrows() > 0 .and. size(data) /= df % nrows()) error stop "data size must match number of rows"

        original_cols = df % ncols()
        call temp_df % new(df % get_max_char_len())

        ! Copy columns before insertion point
        do i = 1, position - 1
            call copy_column_to_df(df, i, temp_df)
        end do

        ! Insert new column
        if (present(header)) then
            call df_append_real(temp_df, data, header)
        else
            call df_append_real(temp_df, data)
        end if

        ! Copy remaining columns
        do i = position, original_cols
            call copy_column_to_df(df, i, temp_df)
        end do

        call df % destroy()
        df = temp_df
    end subroutine df_insert_column_real