This function modifies a table by updating the "stat_" columns with corresponding "add_n_stat_" columns if they exist. If "stat_" columns contain missing values (NA), the function replaces them with the respective "add_n_stat_" column values. Extra "add_n_stat_*" columns are removed after processing.

modify_table_body_func(data)

Arguments

data

A data frame that contains columns named stat_* and optionally add_n_stat_*. The function assumes that stat_* columns are to be updated using add_n_stat_* columns when necessary.

Value

A modified data frame where:

  • stat_* columns are updated to replace NA values with the corresponding values from add_n_stat_*.

  • add_n_stat_* columns are removed after processing.

  • If no add_n_stat_* columns exist, these columns are simply removed.

Details

  • The function identifies columns starting with "add_n_stat_" and attempts to use them to fill missing values in columns matching the pattern "^stat_\d+$".

  • If all required "add_n_stat_*" columns exist in data, they are utilized for this replacement; otherwise, the add_n_stat_* columns are removed without modifications to the stat_* columns.

Examples

# Example data
data <- data.frame(
  n = c(1, 2, 3),
  stat_1 = c(NA, 5, 6),
  stat_2 = c(7, NA, 9),
  add_n_stat_1 = c(10, 11, 12),
  add_n_stat_2 = c(13, 14, 15)
)

# Apply the function
modified_data <- modify_table_body_func(data)
print(modified_data)
#>   stat_1 stat_2
#> 1     10      7
#> 2      5     14
#> 3      6      9