Skip to contents

This function calculates the number of occurrences of a specific drug class in a data frame. The calculation is based on custom conditions specified by the user.

Usage

is_taking_drug_class(
  df,
  class_var_name,
  med_vars,
  last_taken_vars,
  class_condition_fun,
  log_level = "INFO",
  overwrite = FALSE
)

Arguments

df

data.frame The data frame containing medication and last taken information.

class_var_name

character The name of the new variable representing the drug class.

med_vars

character A character vector containing the names of medication variables in the data frame.

last_taken_vars

character A character vector containing the names of last taken variables in the data frame.

class_condition_fun

function A custom condition function that determines whether a medication belongs to the drug class. The function should accept two arguments: med_code (character) and last_taken (numeric). It should return an integer, 1 if the medication belongs to the class, 0 otherwise.

log_level

character The log level for logging messages (default is "INFO").

overwrite

logical Logical value indicating whether to overwrite the 'class_var_name' if it already exists in the data frame (default is FALSE).

Value

data.frame The input data frame 'df' with an additional column representing the drug class.

Details

The 'class_condition_fun' is applied to each pair of medication and last taken variables. The resulting values (0 or 1) are summed for each row, and the sum is stored in the new 'class_var_name' column.

     **Missing Data Codes:**
     - The function handles tagged NAs from the `class_condition_fun` and propagates them.

Examples

# This is a generalized function and requires a user-defined condition function.
# See specific implementations like `is_beta_blocker` for concrete examples.