Append a rowwise summary indicator column
Source:R/derived_variables.R
append_rowwise_summary_indicator.Rd
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 incols
(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"
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)
#> # A tibble: 4 × 5
#> med_1 med_2 med_3 IGNORED_COL on_anti_hypertensive
#> <chr> <chr> <lgl> <chr> <chr>
#> 1 Beta blocker NA NA Beta blocker Yes
#> 2 ACE inhibitor Beta blocker NA Beta blocker Yes
#> 3 Aspirin Aspirin NA Beta blocker No
#> 4 Aspirin ACE inhibitor NA Beta blocker Yes
# print variable label for new column
print(attributes(df$on_anti_hypertensive)$label)
#> [1] "Taking anti-hypertensive medication"