Skip to content

Horizontally concatenate columns into a single string column

Description

Operates in linear time.

Usage

pl$concat_str(..., separator = "", ignore_nulls = FALSE)

Arguments

\<dynamic-dots\> Columns to concatenate into a single string column. Accepts expression input. Strings are parsed as column names, other non-expression inputs are parsed as literals. Non-String columns are cast to String.
separator String that will be used to separate the values of each column.
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(
  a = 1:3,
  b = c("dogs", "cats", NA),
  c = c("play", "swim", "walk")
)
df$with_columns(
  full_sentence = pl$concat_str(
    pl$col("a") * 2L,
    pl$col("b"),
    pl$col("c"),
    separator = " ",
  )
)
#> shape: (3, 4)
#> ┌─────┬──────┬──────┬───────────────┐
#> │ a   ┆ b    ┆ c    ┆ full_sentence │
#> │ --- ┆ ---  ┆ ---  ┆ ---           │
#> │ i32 ┆ str  ┆ str  ┆ str           │
#> ╞═════╪══════╪══════╪═══════════════╡
#> │ 1   ┆ dogs ┆ play ┆ 2 dogs play   │
#> │ 2   ┆ cats ┆ swim ┆ 4 cats swim   │
#> │ 3   ┆ null ┆ walk ┆ null          │
#> └─────┴──────┴──────┴───────────────┘