Factorizes a vector by percentiles

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)
#> x
#>       low    middle      high (Missing) 
#>        24        36        32         8 

split_at_percentile(
  1:100, 
  frac = c(0.25, 0.5, 0.75), 
  labels = c("0-24", "25-49", "50-74", "75-100")
)
#>   [1] 0-24   0-24   0-24   0-24   0-24   0-24   0-24   0-24   0-24   0-24  
#>  [11] 0-24   0-24   0-24   0-24   0-24   0-24   0-24   0-24   0-24   0-24  
#>  [21] 0-24   0-24   0-24   0-24   0-24   25-49  25-49  25-49  25-49  25-49 
#>  [31] 25-49  25-49  25-49  25-49  25-49  25-49  25-49  25-49  25-49  25-49 
#>  [41] 25-49  25-49  25-49  25-49  25-49  25-49  25-49  25-49  25-49  25-49 
#>  [51] 50-74  50-74  50-74  50-74  50-74  50-74  50-74  50-74  50-74  50-74 
#>  [61] 50-74  50-74  50-74  50-74  50-74  50-74  50-74  50-74  50-74  50-74 
#>  [71] 50-74  50-74  50-74  50-74  50-74  75-100 75-100 75-100 75-100 75-100
#>  [81] 75-100 75-100 75-100 75-100 75-100 75-100 75-100 75-100 75-100 75-100
#>  [91] 75-100 75-100 75-100 75-100 75-100 75-100 75-100 75-100 75-100 75-100
#> Levels: 0-24 25-49 50-74 75-100