Return the row indices that would sort the column(s)
Description
Return the row indices that would sort the column(s)
Usage
pl$arg_sort_by(
...,
descending = FALSE,
nulls_last = FALSE,
multithreaded = TRUE,
maintain_order = FALSE
)
Arguments
…
|
\<dynamic-dots \> Column(s) to sort by. Can be character
values indicating column names or Expr(s).
|
descending
|
Sort in descending order. When sorting by multiple columns, this can be specified per column by passing a logical vector. |
nulls_last
|
Place null values last. When sorting by multiple columns, this can be specified per column by passing a logical vector. |
multithreaded
|
Sort using multiple threads. |
maintain_order
|
Whether the order should be maintained if elements are equal. If
TRUE , streaming is not possible and performance might be
worse since this requires a stable search.
|
Value
A polars expression
Examples
library("polars")
# Pass a single column name to compute the arg sort by that column.
df <- pl$DataFrame(
a = c(0, 1, 1, 0),
b = c(3, 2, 3, 2),
c = c(1, 2, 3, 4)
)
df$select(pl$arg_sort_by("a"))
#> shape: (4, 1)
#> ┌─────┐
#> │ a │
#> │ --- │
#> │ u32 │
#> ╞═════╡
#> │ 0 │
#> │ 3 │
#> │ 1 │
#> │ 2 │
#> └─────┘
# Compute the arg sort by multiple columns by either passing a list of
# columns, or by specifying each column as a positional argument.
df$select(pl$arg_sort_by("a", "b", descending = TRUE))
#> shape: (4, 1)
#> ┌─────┐
#> │ a │
#> │ --- │
#> │ u32 │
#> ╞═════╡
#> │ 2 │
#> │ 1 │
#> │ 0 │
#> │ 3 │
#> └─────┘
# Use gather to apply the arg sort to other columns.
df$select(pl$col("c")$gather(pl$arg_sort_by("a")))
#> shape: (4, 1)
#> ┌─────┐
#> │ c │
#> │ --- │
#> │ f64 │
#> ╞═════╡
#> │ 1.0 │
#> │ 4.0 │
#> │ 2.0 │
#> │ 3.0 │
#> └─────┘