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(), fact(), fact2char(), fact_na()

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

mark documentation built on May 29, 2024, 5:13 a.m.