append_rowwise_summary_indicator: Append a rowwise summary indicator column

View source: R/derived_variables.R

append_rowwise_summary_indicatorR Documentation

Append a rowwise summary indicator column

Description

Low level function that appends a (binary) indicator column to a dataframe, summarising rowwise whether any values in a selection of existing columns match one or more specified values.

Usage

append_rowwise_summary_indicator(
  df,
  cols,
  yes_values,
  new_indicator_colname,
  new_indicator_var_label,
  new_indicator_yes_value = "Yes",
  new_indicator_no_value = "No"
)

Arguments

df

Data frame

cols

Character vector of columns to summarise

yes_values

Vector. new_indicator_colname will be "Yes" if any values in cols (rowwise) match one of these values.

new_indicator_colname

Name of new indicator column.

new_indicator_var_label

Character string to set as variable label for new indicator column.

new_indicator_yes_value

Default is "Yes".

new_indicator_no_value

Default is "No"

Value

A data frame with an additional column, named as per new_indicator_colname.

Examples

# example input df
df <- tibble::tribble(
~med_1, ~med_2, ~med_3, ~IGNORED_COL,
"Beta blocker", NA, NA, "Beta blocker",
"ACE inhibitor", "Beta blocker", NA, "Beta blocker",
"Aspirin", "Aspirin", NA, "Beta blocker",
"Aspirin", "ACE inhibitor", NA, "Beta blocker"
)

# add indicator col
df <- append_rowwise_summary_indicator(df = df,
cols = c("med_1", "med_2", "med_3"),
yes_values = c("Beta blocker", "ACE inhibitor"),
new_indicator_colname = "on_anti_hypertensive",
new_indicator_var_label = "Taking anti-hypertensive medication",
new_indicator_yes_value = "Yes",
new_indicator_no_value = "No")

# print result
print(df)

# print variable label for new column
print(attributes(df$on_anti_hypertensive)$label)

rmgpanw/ukbwranglrextra documentation built on Jan. 4, 2023, 1:56 a.m.