Apply a function to a specific row
Extracts all numeric values from a specified row and applies a user-defined function to them. Non-numeric columns are skipped.
@param[in] df The data frame @param[in] row_idx Row index to process @param[in] func Function to apply (must match row_func_real interface) @return Result of applying the function to the row
! Define a function that computes row sum
function row_sum(values, n) result(s)
real(rk), dimension(:), intent(in) :: values
integer, intent(in) :: n
real(rk) :: s
s = sum(values(1:n))
end function
! Apply it
result = df_apply_to_row_real(df, 5, row_sum)
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(data_frame), | intent(in) | :: | df | |||
integer, | intent(in) | :: | row_idx | |||
procedure(row_func_real) | :: | func |
function df_apply_to_row_real(df, row_idx, func) result(output) type(data_frame), intent(in) :: df integer, intent(in) :: row_idx procedure(row_func_real) :: func real(rk) :: output real(rk), dimension(:), allocatable :: row_values integer :: i, n_numeric, col_type type(column) :: col ! Count numeric columns n_numeric = 0 do i = 1, df % ncols() col = df % get_data_col(i) col_type = col % dtype if (col_type == REAL_NUM .or. col_type == INTEGER_NUM) then n_numeric = n_numeric + 1 end if end do ! Allocate and fill row values allocate (row_values(n_numeric)) n_numeric = 0 do i = 1, df % ncols() col = df % get_data_col(i) col_type = col % dtype if (col_type == REAL_NUM) then n_numeric = n_numeric + 1 row_values(n_numeric) = col % getr(row_idx) else if (col_type == INTEGER_NUM) then n_numeric = n_numeric + 1 row_values(n_numeric) = real(col % geti(row_idx), rk) end if end do ! Apply function output = func(row_values, n_numeric) deallocate (row_values) end function df_apply_to_row_real