Skip to content

Join elements in every sub-array

Description

Join all string items in a sub-array and place a separator between them. This only works if the inner type of the array is String.

Usage

<Expr>$arr$join(separator, ..., ignore_nulls = FALSE)

Arguments

separator String to separate the items with. Can be an Expr. Strings are not parsed as columns.
These dots are for future extensions and must be empty.
ignore_nulls If FALSE (default), null values will be propagated, i.e. if the row contains any null values, the output is null.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  values = list(c("a", "b", "c"), c("x", "y", "z"), c("e", NA, NA)),
  separator = c("-", "+", "/"),
)$cast(values = pl$Array(pl$String, 3))
df$with_columns(
  join_with_expr = pl$col("values")$arr$join(pl$col("separator")),
  join_with_lit = pl$col("values")$arr$join(" "),
  join_ignore_null = pl$col("values")$arr$join(" ", ignore_nulls = TRUE)
)
#> shape: (3, 5)
#> ┌───────────────────┬───────────┬────────────────┬───────────────┬──────────────────┐
#> │ values            ┆ separator ┆ join_with_expr ┆ join_with_lit ┆ join_ignore_null │
#> │ ---               ┆ ---       ┆ ---            ┆ ---           ┆ ---              │
#> │ array[str, 3]     ┆ str       ┆ str            ┆ str           ┆ str              │
#> ╞═══════════════════╪═══════════╪════════════════╪═══════════════╪══════════════════╡
#> │ ["a", "b", "c"]   ┆ -         ┆ a-b-c          ┆ a b c         ┆ a b c            │
#> │ ["x", "y", "z"]   ┆ +         ┆ x+y+z          ┆ x y z         ┆ x y z            │
#> │ ["e", null, null] ┆ /         ┆ null           ┆ null          ┆ e                │
#> └───────────────────┴───────────┴────────────────┴───────────────┴──────────────────┘