Skip to content

Convert categorical variables into dummy/indicator variables

Description

Convert categorical variables into dummy/indicator variables

Usage

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

Arguments

\<dynamic-dots\> Column name(s) that should be converted to dummy variables. If empty (default), convert all columns.
separator Separator/delimiter used when generating column names.
drop_first Remove the first category from the variables being encoded.

Value

A polars DataFrame

Examples

library("polars")

df <- pl$DataFrame(
  foo = c(1L, 2L),
  bar = c(3L, 4L),
  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("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   │
#> └───────┴───────┴───────┴───────┴─────┘