Skip to content

Unstack a long table to a wide form without doing an aggregation

Description

This can be much faster than a pivot, because it can skip the grouping phase.

Usage

<DataFrame>$unstack(
  ...,
  step,
  how = c("vertical", "horizontal"),
  fill_values = NULL
)

Arguments

\<dynamic-dots\> Column name(s) and selector(s) to include in the operation. If empty, use all columns.
step Number of rows in the unstacked frame.
how Direction of the unstack. Must be one of “vertical” or “horizontal”.
fill_values Fill values that don’t fit the new size with this value. This can be a scalar value or a named list of the sort list(\ = \). See examples.

Value

A polars DataFrame

Examples

library("polars")

df <- pl$DataFrame(x = LETTERS[1:8], y = 1:8)$with_columns(
  z = pl$int_ranges(pl$col("y"), pl$col("y") + 2, dtype = pl$UInt8)
)
df
#> shape: (8, 3)
#> ┌─────┬─────┬──────────┐
#> │ x   ┆ y   ┆ z        │
#> │ --- ┆ --- ┆ ---      │
#> │ str ┆ i32 ┆ list[u8] │
#> ╞═════╪═════╪══════════╡
#> │ A   ┆ 1   ┆ [1, 2]   │
#> │ B   ┆ 2   ┆ [2, 3]   │
#> │ C   ┆ 3   ┆ [3, 4]   │
#> │ D   ┆ 4   ┆ [4, 5]   │
#> │ E   ┆ 5   ┆ [5, 6]   │
#> │ F   ┆ 6   ┆ [6, 7]   │
#> │ G   ┆ 7   ┆ [7, 8]   │
#> │ H   ┆ 8   ┆ [8, 9]   │
#> └─────┴─────┴──────────┘
df$unstack(step = 4, how = "vertical")
#> shape: (4, 6)
#> ┌─────┬─────┬─────┬─────┬──────────┬──────────┐
#> │ x_0 ┆ x_1 ┆ y_0 ┆ y_1 ┆ z_0      ┆ z_1      │
#> │ --- ┆ --- ┆ --- ┆ --- ┆ ---      ┆ ---      │
#> │ str ┆ str ┆ i32 ┆ i32 ┆ list[u8] ┆ list[u8] │
#> ╞═════╪═════╪═════╪═════╪══════════╪══════════╡
#> │ A   ┆ E   ┆ 1   ┆ 5   ┆ [1, 2]   ┆ [5, 6]   │
#> │ B   ┆ F   ┆ 2   ┆ 6   ┆ [2, 3]   ┆ [6, 7]   │
#> │ C   ┆ G   ┆ 3   ┆ 7   ┆ [3, 4]   ┆ [7, 8]   │
#> │ D   ┆ H   ┆ 4   ┆ 8   ┆ [4, 5]   ┆ [8, 9]   │
#> └─────┴─────┴─────┴─────┴──────────┴──────────┘
df$unstack(step = 2, how = "horizontal")
#> shape: (4, 6)
#> ┌─────┬─────┬─────┬─────┬──────────┬──────────┐
#> │ x_0 ┆ x_1 ┆ y_0 ┆ y_1 ┆ z_0      ┆ z_1      │
#> │ --- ┆ --- ┆ --- ┆ --- ┆ ---      ┆ ---      │
#> │ str ┆ str ┆ i32 ┆ i32 ┆ list[u8] ┆ list[u8] │
#> ╞═════╪═════╪═════╪═════╪══════════╪══════════╡
#> │ A   ┆ B   ┆ 1   ┆ 2   ┆ [1, 2]   ┆ [2, 3]   │
#> │ C   ┆ D   ┆ 3   ┆ 4   ┆ [3, 4]   ┆ [4, 5]   │
#> │ E   ┆ F   ┆ 5   ┆ 6   ┆ [5, 6]   ┆ [6, 7]   │
#> │ G   ┆ H   ┆ 7   ┆ 8   ┆ [7, 8]   ┆ [8, 9]   │
#> └─────┴─────┴─────┴─────┴──────────┴──────────┘
df$unstack(cs$numeric(), step = 5, fill_values = 0)
#> shape: (5, 2)
#> ┌─────┬─────┐
#> │ y_0 ┆ y_1 │
#> │ --- ┆ --- │
#> │ i32 ┆ i32 │
#> ╞═════╪═════╡
#> │ 1   ┆ 6   │
#> │ 2   ┆ 7   │
#> │ 3   ┆ 8   │
#> │ 4   ┆ 0   │
#> │ 5   ┆ 0   │
#> └─────┴─────┘
df$unstack("x", "y", step = 5, fill_values = list(y = 999, x = "foo"))
#> shape: (5, 4)
#> ┌─────┬─────┬─────┬─────┐
#> │ x_0 ┆ x_1 ┆ y_0 ┆ y_1 │
#> │ --- ┆ --- ┆ --- ┆ --- │
#> │ str ┆ str ┆ i32 ┆ i32 │
#> ╞═════╪═════╪═════╪═════╡
#> │ A   ┆ F   ┆ 1   ┆ 6   │
#> │ B   ┆ G   ┆ 2   ┆ 7   │
#> │ C   ┆ H   ┆ 3   ┆ 8   │
#> │ D   ┆ foo ┆ 4   ┆ 999 │
#> │ E   ┆ foo ┆ 5   ┆ 999 │
#> └─────┴─────┴─────┴─────┘