Dichotomize vector values at their median

split_at_median(
  x,
  labels = c("low", "high"),
  type = "higher",
  explicit_na = NA
)

Arguments

x

A vector

labels

Vector with two factor labels.

type

"higher" will split group at above median (vs. equal and below) and "lower" will split group below median (vs. equal and above)

explicit_na

If not NA, NAs will be recoded as a factor level of the provided name. If TRUE, the name will default to '(Missing)'.

Value

A vector of type factor with two levels.

Examples

x <- sample(c(1:10, NA), 100, replace = TRUE)
x <- split_at_median(x, explicit_na = TRUE)
table(x)
#> x
#>       low      high (Missing) 
#>        40        52         8