Skip to content

Set values outside the given boundaries to the boundary value

Description

This method only works for numeric and temporal columns. To clip other data types, consider writing a when-then-otherwise expression.

Usage

<Expr>$clip(lower_bound = NULL, upper_bound = NULL)

Arguments

lower_bound Lower bound. Accepts expression input. Non-expression inputs are parsed as literals.
upper_bound Upper bound. Accepts expression input. Non-expression inputs are parsed as literals.

Details

This method only works for numeric and temporal columns. To clip other data types, consider writing a when-then-otherwise expression.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(a = c(-50, 5, 50, NA))

# Specifying both a lower and upper bound:
df$with_columns(
  clip = pl$col("a")$clip(1, 10)
)
#> shape: (4, 2)
#> ┌───────┬──────┐
#> │ a     ┆ clip │
#> │ ---   ┆ ---  │
#> │ f64   ┆ f64  │
#> ╞═══════╪══════╡
#> │ -50.0 ┆ 1.0  │
#> │ 5.0   ┆ 5.0  │
#> │ 50.0  ┆ 10.0 │
#> │ null  ┆ null │
#> └───────┴──────┘
# Specifying only a single bound:
df$with_columns(
  clip = pl$col("a")$clip(upper_bound = 10)
)
#> shape: (4, 2)
#> ┌───────┬───────┐
#> │ a     ┆ clip  │
#> │ ---   ┆ ---   │
#> │ f64   ┆ f64   │
#> ╞═══════╪═══════╡
#> │ -50.0 ┆ -50.0 │
#> │ 5.0   ┆ 5.0   │
#> │ 50.0  ┆ 10.0  │
#> │ null  ┆ null  │
#> └───────┴───────┘