as_ordered: Ordered

View source: R/fact.R

as_orderedR Documentation

Ordered

Description

As ordered

Usage

as_ordered(x)

## Default S3 method:
as_ordered(x)

Arguments

x

A vector of values

Details

Simple implementation of ordered. If x is ordered it is simply returned. If x is a factor the ordered class is added. Otherwise, x is made into a factor with fact() and then the ordered class is added. Unlike just fact, ordered will replace the NA levels with NA_integer_ to work appropriately with other functions.

Value

An ordered vector

See Also

fact()

Other factors: char2fact(), drop_levels(), fact2char(), fact_na(), fact()

Examples

x <- c("a", NA, "b")
x <- fact(x)
str(x) # NA is 3L

y <- x
class(y) <- c("ordered", class(y))
max(y)
max(y, na.rm = TRUE) # returns NA -- bad

# as_ordered() removes the NA level
x <- as_ordered(x)
str(x)
max(x, na.rm = TRUE) # returns b -- correct

jmbarbone/jordan documentation built on April 1, 2024, 7:46 p.m.