Sort a DataFrame by the given columns
Description
Sort a DataFrame by the given columns
Usage
<DataFrame>$sort(
...,
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 DataFrame
Examples
library("polars")
df <- pl$DataFrame(
a = c(1, 2, NA, 4),
b = c(6, 5, 4, 3),
c = c("a", "c", "b", "a")
)
# Pass a single column name to sort by that column.
df$sort("a")
#> shape: (4, 3)
#> ┌──────┬─────┬─────┐
#> │ a ┆ b ┆ c │
#> │ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ str │
#> ╞══════╪═════╪═════╡
#> │ null ┆ 4.0 ┆ b │
#> │ 1.0 ┆ 6.0 ┆ a │
#> │ 2.0 ┆ 5.0 ┆ c │
#> │ 4.0 ┆ 3.0 ┆ a │
#> └──────┴─────┴─────┘
# Sorting by expressions is also supported
df$sort(pl$col("a") + pl$col("b") * 2, nulls_last = TRUE)
#> shape: (4, 3)
#> ┌──────┬─────┬─────┐
#> │ a ┆ b ┆ c │
#> │ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ str │
#> ╞══════╪═════╪═════╡
#> │ 4.0 ┆ 3.0 ┆ a │
#> │ 2.0 ┆ 5.0 ┆ c │
#> │ 1.0 ┆ 6.0 ┆ a │
#> │ null ┆ 4.0 ┆ b │
#> └──────┴─────┴─────┘
#> shape: (4, 3)
#> ┌──────┬─────┬─────┐
#> │ a ┆ b ┆ c │
#> │ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ str │
#> ╞══════╪═════╪═════╡
#> │ 2.0 ┆ 5.0 ┆ c │
#> │ null ┆ 4.0 ┆ b │
#> │ 4.0 ┆ 3.0 ┆ a │
#> │ 1.0 ┆ 6.0 ┆ a │
#> └──────┴─────┴─────┘
# Or use positional arguments to sort by multiple columns in the same way
df$sort("c", "a", descending = c(FALSE, TRUE))
#> shape: (4, 3)
#> ┌──────┬─────┬─────┐
#> │ a ┆ b ┆ c │
#> │ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ str │
#> ╞══════╪═════╪═════╡
#> │ 4.0 ┆ 3.0 ┆ a │
#> │ 1.0 ┆ 6.0 ┆ a │
#> │ null ┆ 4.0 ┆ b │
#> │ 2.0 ┆ 5.0 ┆ c │
#> └──────┴─────┴─────┘