Sort the LazyFrame by the given columns
Description
Sort the LazyFrame by the given columns
Usage
<LazyFrame>$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 LazyFrame
Examples
library("polars")
lf <- pl$LazyFrame(
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.
lf$sort("a")$collect()
#> 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
lf$sort(pl$col("a") + pl$col("b") * 2, nulls_last = TRUE)$collect()
#> 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 │
#> └──────┴─────┴─────┘
# Sort by multiple columns by passing a vector of columns
lf$sort(c("c", "a"), descending = TRUE)$collect()
#> 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
lf$sort("c", "a", descending = c(FALSE, TRUE))$collect()
#> 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 │
#> └──────┴─────┴─────┘