Skip to contents

Converts continuous variables into a factor that is split at a chosen cutpoint.

Usage

dichotomize(
  x,
  location = function(x) median(x, na.rm = TRUE),
  cutpoint = "below",
  less_than_symbol = c(below = "<", above = "≤"),
  greater_than_symbol = c(above = ">", below = "≥"),
  digits = 2
)

Arguments

x

A numeric vector.

location

A function which will be passed x and returns a numeric of length one, or a numeric of length one.

cutpoint

A character string of either "below" or "above".

less_than_symbol

A named character vector of symbols to use for values less than the chosen location. Includes values appropriate for both "below" and "above" cutpoints.

greater_than_symbol

A named character vector of symbols to use for values greater than the chosen location. Includes values appropriate for both "below" and "above" cutpoints.

digits

An integer to use for rounding the output.

Value

factor with 2 levels

Examples

#----------------------------------------------------------------------------
# dichotomize() examples
#----------------------------------------------------------------------------
library(bkstat)

x <- rnorm(10)
dichotomize(x, cutpoint = "below")
#>  [1] ≥0.1 <0.1 <0.1 ≥0.1 <0.1 ≥0.1 <0.1 <0.1 ≥0.1 ≥0.1
#> Levels: <0.1 ≥0.1
dichotomize(x, cutpoint = "above")
#>  [1] >0.1 ≤0.1 ≤0.1 >0.1 ≤0.1 >0.1 ≤0.1 ≤0.1 >0.1 >0.1
#> Levels: ≤0.1 >0.1