as_ordered: Ordered

Description Usage Arguments Details Value Examples

View source: R/fact.R

Description

As ordered

Usage

1
2
3
4
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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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 Sept. 18, 2021, 5:07 p.m.