Skip to content

Rename the expression

Description

Rename the expression

Usage

<Expr>$alias(name)

Arguments

name The new name.

Value

A polars expression

Examples

library("polars")

# Rename an expression to avoid overwriting an existing column
df <- pl$DataFrame(a = 1:3, b = c("x", "y", "z"))
df$with_columns(
  pl$col("a") + 10,
  pl$col("b")$str$to_uppercase()$alias("c")
)
#> shape: (3, 3)
#> ┌──────┬─────┬─────┐
#> │ a    ┆ b   ┆ c   │
#> │ ---  ┆ --- ┆ --- │
#> │ f64  ┆ str ┆ str │
#> ╞══════╪═════╪═════╡
#> │ 11.0 ┆ x   ┆ X   │
#> │ 12.0 ┆ y   ┆ Y   │
#> │ 13.0 ┆ z   ┆ Z   │
#> └──────┴─────┴─────┘
# Overwrite the default name of literal columns to prevent errors due to
# duplicate column names.
df$with_columns(
  pl$lit(TRUE)$alias("c"),
  pl$lit(4)$alias("d")
)
#> shape: (3, 4)
#> ┌─────┬─────┬──────┬─────┐
#> │ a   ┆ b   ┆ c    ┆ d   │
#> │ --- ┆ --- ┆ ---  ┆ --- │
#> │ i32 ┆ str ┆ bool ┆ f64 │
#> ╞═════╪═════╪══════╪═════╡
#> │ 1   ┆ x   ┆ true ┆ 4.0 │
#> │ 2   ┆ y   ┆ true ┆ 4.0 │
#> │ 3   ┆ z   ┆ true ┆ 4.0 │
#> └─────┴─────┴──────┴─────┘