order_by: Order a factor by the levels of another variable.

Description Usage Arguments Details Value Examples

View source: R/order_by.R

Description

The factor and the order must be of the same length and each value of factor must correspond with one value of order.

Usage

1

Arguments

factor

A factor or character vector.

order

A numeric variable (or coercible to numeric).

desc

Set to TRUE to order descending.

Details

This is useful when you have unique values of factor and numeric, but can be useful in other circumstances when another variable corresponds to the ordering of the factor.

Ties are arbitrarily ordered for unique factors.

Value

An ordered factor.

Examples

1
2
3
4
5
# Ordering a factor where the values are unique
order_by(letters[1:3], 1:3)

# Ordering a factor by a variable that provides the levels
order_by(c('a', 'a', 'b'), c(1, 1, 2))

mhairi/ordered documentation built on May 22, 2019, 8:57 p.m.