Skip to contents

Create 'range labels' for grouping and tabulating a continuous variable

Usage

range_labels(
  x,
  width,
  start = NA,
  sep = " - ",
  explicit_zero = TRUE,
  suffix = "",
  include = c("upper", "lower", "integer"),
  ordered = TRUE
)

Arguments

x

a numeric vector

width

Desidered label width - numeric

start

The number from which to start the lowest range label

sep

The character used to separate values within each label

explicit_zero

Logical; should zeroes have a label all to themselves? They do by default.

suffix

Optional suffix to append to each label (e.g. "%")

include

Which side of each range should be included. One of 'upper', 'lower' or 'integer' - the latter is a special case that deals exclusively with integer ranges and otherwise crashes.

ordered

Logical; should output be an ordered factor?

Value

A character vector of labels

Examples

range_labels(1:10, width = 3)
#>  [1] 1 - <4   1 - <4   1 - <4   4 - <7   4 - <7   4 - <7   7 - <10  7 - <10 
#>  [9] 7 - <10  10 - <13
#> Levels: 1 - <4 < 4 - <7 < 7 - <10 < 10 - <13
range_labels(1:10, width = 3, include = "lower", start = 0, explicit_zero = TRUE)
#>  [1] >0 - 3  >0 - 3  >0 - 3  >3 - 6  >3 - 6  >3 - 6  >6 - 9  >6 - 9  >6 - 9 
#> [10] >9 - 12
#> Levels: >-3 - 0 < >0 - 3 < >3 - 6 < >6 - 9 < >9 - 12