Learn R Programming

vctrs (version 0.6.5)

vec_order: Order and sort vectors

Description

Order and sort vectors

Usage

vec_order(
  x,
  ...,
  direction = c("asc", "desc"),
  na_value = c("largest", "smallest")
)

vec_sort( x, ..., direction = c("asc", "desc"), na_value = c("largest", "smallest") )

Value

  • vec_order() an integer vector the same size as x.

  • vec_sort() a vector with the same size and type as x.

Arguments

x

A vector

...

These dots are for future extensions and must be empty.

direction

Direction to sort in. Defaults to ascending.

na_value

Should NAs be treated as the largest or smallest values?

Differences with <code><a href="/link/order()?package=vctrs&version=0.6.5" data-mini-rdoc="vctrs::order()">order()</a></code>

Unlike the na.last argument of order() which decides the positions of missing values irrespective of the decreasing argument, the na_value argument of vec_order() interacts with direction. If missing values are considered the largest value, they will appear last in ascending order, and first in descending order.

Dependencies of <code>vec_order()</code>

  • vec_proxy_order()

Dependencies of <code>vec_sort()</code>

  • vec_proxy_order()

  • vec_order()

  • vec_slice()

Examples

Run this code
x <- round(c(runif(9), NA), 3)
vec_order(x)
vec_sort(x)
vec_sort(x, direction = "desc")

# Can also handle data frames
df <- data.frame(g = sample(2, 10, replace = TRUE), x = x)
vec_order(df)
vec_sort(df)
vec_sort(df, direction = "desc")

# Missing values interpreted as largest values are last when
# in increasing order:
vec_order(c(1, NA), na_value = "largest", direction = "asc")
vec_order(c(1, NA), na_value = "largest", direction = "desc")

Run the code above in your browser using DataLab