Skip to content

Find indices where elements should be inserted to maintain order

Description

This returns -1 if x is lower than 0, 0 if x == 0, and 1 if x is greater than 0.

Usage

<Expr>$search_sorted(element, side = c("any", "left", "right"))

Arguments

element Expression or scalar value.
side Must be one of the following:
  • “any”: the index of the first suitable location found is given;
  • “left”: the index of the leftmost suitable location found is given;
  • “right”: the index the rightmost suitable location found is given.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(values = c(1, 2, 3, 5))
df$select(
  zero = pl$col("values")$search_sorted(0),
  three = pl$col("values")$search_sorted(3),
  six = pl$col("values")$search_sorted(6),
)
#> shape: (1, 3)
#> ┌──────┬───────┬─────┐
#> │ zero ┆ three ┆ six │
#> │ ---  ┆ ---   ┆ --- │
#> │ u32  ┆ u32   ┆ u32 │
#> ╞══════╪═══════╪═════╡
#> │ 0    ┆ 2     ┆ 4   │
#> └──────┴───────┴─────┘