Skip to content

Horizontally concatenate columns into a single array column

Source code

Description

[Experimental]

Usage

pl$concat_arr(...)

Arguments

\<dynamic-dots\> Columns to concatenate into a single array column. Accepts expression input. Strings are parsed as column names, other non-expression inputs are parsed as literals.

Value

A polars expression

Examples

library("polars")

# Concatenate two existing array columns.
df <- pl$DataFrame(a = list(1:2, 3:4, 5:6), b = list(4, 1, NA))$cast(
  a = pl$Array(pl$Int64, 2),
  b = pl$Array(pl$Int64, 1)
)

df$with_columns(concat_arr = pl$concat_arr("a", "b"))
#> shape: (3, 3)
#> ┌───────────────┬───────────────┬───────────────┐
#> │ a             ┆ b             ┆ concat_arr    │
#> │ ---           ┆ ---           ┆ ---           │
#> │ array[i64, 2] ┆ array[i64, 1] ┆ array[i64, 3] │
#> ╞═══════════════╪═══════════════╪═══════════════╡
#> │ [1, 2]        ┆ [4]           ┆ [1, 2, 4]     │
#> │ [3, 4]        ┆ [1]           ┆ [3, 4, 1]     │
#> │ [5, 6]        ┆ [null]        ┆ [5, 6, null]  │
#> └───────────────┴───────────────┴───────────────┘
# Concatenate two existing non-array columns.
df <- pl$DataFrame(a = c(NA, 5, 6), b = c(6, 5, NA))
df$with_columns(concat_arr = pl$concat_arr("a", "b"))
#> shape: (3, 3)
#> ┌──────┬──────┬───────────────┐
#> │ a    ┆ b    ┆ concat_arr    │
#> │ ---  ┆ ---  ┆ ---           │
#> │ f64  ┆ f64  ┆ array[f64, 2] │
#> ╞══════╪══════╪═══════════════╡
#> │ null ┆ 6.0  ┆ [null, 6.0]   │
#> │ 5.0  ┆ 5.0  ┆ [5.0, 5.0]    │
#> │ 6.0  ┆ null ┆ [6.0, null]   │
#> └──────┴──────┴───────────────┘
# Concatenate mixed array and non-array columns.
df <- pl$DataFrame(a = list(NA, 5L, 6L), b = c(6L, 5L, NA))$cast(
  a = pl$Array(pl$Int32, 1)
)
df$with_columns(concat_arr = pl$concat_arr("a", "b"))
#> shape: (3, 3)
#> ┌───────────────┬──────┬───────────────┐
#> │ a             ┆ b    ┆ concat_arr    │
#> │ ---           ┆ ---  ┆ ---           │
#> │ array[i32, 1] ┆ i32  ┆ array[i32, 2] │
#> ╞═══════════════╪══════╪═══════════════╡
#> │ [null]        ┆ 6    ┆ [null, 6]     │
#> │ [5]           ┆ 5    ┆ [5, 5]        │
#> │ [6]           ┆ null ┆ [6, null]     │
#> └───────────────┴──────┴───────────────┘
# Unit-length columns are broadcasted:
df$with_columns(concat_arr = pl$concat_arr("a", pl$sum("b")))
#> shape: (3, 3)
#> ┌───────────────┬──────┬───────────────┐
#> │ a             ┆ b    ┆ concat_arr    │
#> │ ---           ┆ ---  ┆ ---           │
#> │ array[i32, 1] ┆ i32  ┆ array[i32, 2] │
#> ╞═══════════════╪══════╪═══════════════╡
#> │ [null]        ┆ 6    ┆ [null, 11]    │
#> │ [5]           ┆ 5    ┆ [5, 11]       │
#> │ [6]           ┆ null ┆ [6, 11]       │
#> └───────────────┴──────┴───────────────┘