Skip to content

Convert variables into dummy/indicator variables

Source code

Description

Convert variables into dummy/indicator variables

Usage

<DataFrame>$to_dummies(columns = NULL, ..., separator = "_", drop_first = FALSE)

Arguments

columns Column name(s) or selector(s) that should be converted to dummy variables. If NULL (default), convert all columns.
Ignored.
separator Separator/delimiter used when generating column names.
drop_first Remove the first category from the variables being encoded.

Value

A DataFrame

Examples

library("polars")

df = pl$DataFrame(foo = 1:2, bar = 3:4, ham = c("a", "b"))

df$to_dummies()
#> shape: (2, 6)
#> ┌───────┬───────┬───────┬───────┬───────┬───────┐
#> │ foo_1 ┆ foo_2 ┆ bar_3 ┆ bar_4 ┆ ham_a ┆ ham_b │
#> │ ---   ┆ ---   ┆ ---   ┆ ---   ┆ ---   ┆ ---   │
#> │ u8    ┆ u8    ┆ u8    ┆ u8    ┆ u8    ┆ u8    │
#> ╞═══════╪═══════╪═══════╪═══════╪═══════╪═══════╡
#> │ 1     ┆ 0     ┆ 1     ┆ 0     ┆ 1     ┆ 0     │
#> │ 0     ┆ 1     ┆ 0     ┆ 1     ┆ 0     ┆ 1     │
#> └───────┴───────┴───────┴───────┴───────┴───────┘
df$to_dummies(drop_first = TRUE)
#> shape: (2, 3)
#> ┌───────┬───────┬───────┐
#> │ foo_2 ┆ bar_4 ┆ ham_b │
#> │ ---   ┆ ---   ┆ ---   │
#> │ u8    ┆ u8    ┆ u8    │
#> ╞═══════╪═══════╪═══════╡
#> │ 0     ┆ 0     ┆ 0     │
#> │ 1     ┆ 1     ┆ 1     │
#> └───────┴───────┴───────┘
df$to_dummies(c("foo", "bar"), separator = "::")
#> shape: (2, 5)
#> ┌────────┬────────┬────────┬────────┬─────┐
#> │ foo::1 ┆ foo::2 ┆ bar::3 ┆ bar::4 ┆ ham │
#> │ ---    ┆ ---    ┆ ---    ┆ ---    ┆ --- │
#> │ u8     ┆ u8     ┆ u8     ┆ u8     ┆ str │
#> ╞════════╪════════╪════════╪════════╪═════╡
#> │ 1      ┆ 0      ┆ 1      ┆ 0      ┆ a   │
#> │ 0      ┆ 1      ┆ 0      ┆ 1      ┆ b   │
#> └────────┴────────┴────────┴────────┴─────┘