Learn R Programming

mark (version 0.8.2)

as_ordered: Ordered

Description

As ordered

Usage

as_ordered(x)

# S3 method for default as_ordered(x)

Value

An ordered vector

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.

See Also

fact()

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

Examples

Run this code
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

Run the code above in your browser using DataLab