Splits a numeric vector into two groups at the median value. Values above (or below) the median are assigned to one group, values equal to or below (or above) the median are assigned to the other group. Optionally, missing values can be assigned to a separate factor level.

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.

Details

This function is a wrapper around split_at_percentile() with the percentile fixed at 0.5 (the median). See split_at_percentile() for more details.

Examples

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