split_at_percentile: Factorizes a vector by percentiles

View source: R/split_at_percentile.R

split_at_percentileR Documentation

Factorizes a vector by percentiles

Description

Factorizes a vector by percentiles

Usage

split_at_percentile(
  x,
  frac = c(1/3, 2/3),
  labels = c("low", "middle", "high"),
  type = "higher",
  explicit_na = NA
)

Arguments

x

A vector

frac

Fractions to split at (e.g., c(1/3, 2/3) will result in three groups and 0.5 will result in a median split)

labels

Vector with factor labels.

type

"higher" will split group below fraction and last group equal above last fraction. and "lower" will split group below fraction (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_percentile(x, explicit_na = TRUE)
table(x)

split_at_percentile(
  1:100, 
  frac = c(0.25, 0.5, 0.75), 
  labels = c("0-24", "25-49", "50-74", "75-100")
)


jazznbass/wmisc documentation built on Oct. 29, 2024, 5:42 p.m.