Skip to contents

This function dichotomizes (0/1) a variable by applying a function to it and checking whether each element equals the function's return value.

Usage

dichotomize(df, vars, value_fn = max, na.rm = TRUE, ...)

Arguments

df

data.frame

vars

(character) variable names to dichotomize

value_fn

function that returns desired value for 1 (default max())

na.rm

(logical) whether to remove NAs before applying value_fn (default TRUE)

...

(optional) additional arguments passed to value_fn

Value

df with vars dichotomized

Examples

df <- df |> dichotomize(vars="x1")
#> [1] "Making 0-1 numeric by matching to value obtained with value_fn() (1) or else (0)"