reorder2: reorder with respect to multiple vectors

View source: R/reorder2.R

reorder2R Documentation

reorder with respect to multiple vectors

Description

Like reorder() but takes multiple ordering arguments. The reordering can be with respect to expressions.

Usage

reorder2(
  x,
  ...,
  as = c("factor", "order", "rank", "sorted"),
  decreasing = FALSE,
  method = "auto"
)

Arguments

x

The vector to be reordered.

...

vectors or expressions of vectors to reorder by.

as

One of the following strings and their results

"factor" x as a factor with levels according to ... "order" the sorting order of x according to ... "rank" rank of x according to ... "sorted" x sorted according to ...

decreasing

passed to order

method

passed to order

Details

reorder2 uses order() and passes decreasing and method along. Notice, the default is increasing order. For logicals this means negative comes first (see example).

'reorder2()' does not return the scores attribute (which is unlike 'reorder()').

Use rank when assigning value to an ordering column in existing df.

Value

A factor, character, or numeric

Examples

df <- data.frame(char=letters[1:5],num=c(3,2,2,1,3),logic=c(TRUE,FALSE,TRUE,TRUE,FALSE))
reorder2(df$char,df$num,df$logic)
reorder2(df$char,df$num,df$logic,as="sorted")
reorder2(df$char,df$num,df$logic,as="order")
reorder2(df$char,df$num,df$logic,as="rank")

NMdata documentation built on June 17, 2026, 9:06 a.m.