Constructors and methods for an ordered categorical data type. v_ordered and ord are synonyms that each create a new v_ordered object subclassed from vctrs_vctr, ordered, and factor.

Support: see factor
Prototype: integer

v_ordered(
  x = factor(ordered = TRUE),
  internal_name = "",
  context,
  auto_compute_summary = auto_compute_default,
  extra_descriptors = list()
)

ord(
  x = factor(ordered = TRUE),
  internal_name = "",
  context,
  auto_compute_summary = auto_compute_default,
  extra_descriptors = list()
)

is_ordered(x)

as_ordered(x)

# S3 method for v_ordered
as_canonical(x)

Arguments

x

a factor

internal_name

the internal name of the variable

context

a context

auto_compute_summary

an indicator of whether the data_summary is automatically computed whenever a vector is initialized, subset, or concatenated. Defaults to TRUE. If this option is set to FALSE, then get_data_summary is the only way to compute the summary. The data_summary_l lens will return an empty data_summary.

extra_descriptors

A list of descriptors functions appended to the default descriptors.

Examples

# Example data
src_fct <- factor(c("a", "bb", "a", "ccc", NA_character_), ordered = TRUE)

# Constructor for the `v_ordered` class. One can also use `ord` which is a
# synonym for the `v_ordered` function.
v <- v_ordered(
  x = src_fct,
  internal_name = "v_example",
  context = context(
    short_label = "important_var",
    long_label  = "Very important variable"
  ),
  extra_descriptors = list()
)

# Helper functions and methods
is_ordered(v)
#> [1] TRUE
as_ordered(src_fct)
#> <ordered[5]>
#> [1] a    bb   a    ccc  <NA>
#> Proportions: a: 40% bb: 20% ccc: 20% NA: 20%; Missing = 1
as.character(v)
#> [1] "a"   "bb"  "a"   "ccc" NA   
as_canonical(v)
#> [1] a    bb   a    ccc  <NA>
#> Levels: a < bb < ccc