Like reorder() but takes multiple ordering arguments. The reordering can be with respect to expressions.
reorder2(
x,
...,
as = c("factor", "order", "rank", "sorted"),
decreasing = FALSE,
method = "auto"
)The vector to be reordered.
vectors or expressions of vectors to reorder by.
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 ...
passed to order
passed to order
A factor, character, or numeric
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.
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)
#> [1] a b c d e
#> Levels: d b c e a
reorder2(df$char,df$num,df$logic,as="sorted")
#> [1] "d" "b" "c" "e" "a"
reorder2(df$char,df$num,df$logic,as="order")
#> [1] 4 2 3 5 1
reorder2(df$char,df$num,df$logic,as="rank")
#> [1] 5 2 3 1 4