This function transforms selected columns in a dataframe to numeric, calculates their row-wise sums, and then creates a binary indicator column. The binary column is determined by whether the row sum is equal to or greater than 1. It dynamically generates the names for the row sum and binary columns based on input parameters.

create_from_rowsums(df, col_names, risk_behavior)

Arguments

df

A dataframe containing the columns to be transformed and summed.

col_names

Vector of column names to be converted to numeric and summed.

risk_behavior

The name to be assigned to the binary indicator column.

Value

A modified dataframe with the original columns replaced by a binary indicator column, reflecting the presence (1) or absence (0) of the aggregated risk behavior, based on the sum of the specified columns.

Details

The function performs the following steps:

  1. Converts the specified columns (col_names) to numeric.

  2. Calculates the row-wise sum of these columns, handling NA values.

  3. Creates a binary indicator column (risk_behavior) where 1 indicates a row sum >= 1, and 0 indicates a row sum < 1.

  4. Removes the original columns used for row sums, retaining only the binary indicator and other existing dataframe columns.