Skip to content

Apply the OR logical rowwise

Source code

Description

Apply the OR logical rowwise

Usage

pl$any_horizontal(...)

Arguments

Columns to concatenate into a single string column. Accepts expressions. Strings are parsed as column names, other non-expression inputs are parsed as literals.

Value

Expr

Examples

library(polars)

df = pl$DataFrame(
  a = c(FALSE, FALSE, NA, NA),
  b = c(TRUE, FALSE, NA, NA),
  c = c(TRUE, FALSE, NA, TRUE)
)
df
#> shape: (4, 3)
#> ┌───────┬───────┬───────┐
#> │ a     ┆ b     ┆ c     │
#> │ ---   ┆ ---   ┆ ---   │
#> │ bool  ┆ bool  ┆ bool  │
#> ╞═══════╪═══════╪═══════╡
#> │ false ┆ true  ┆ true  │
#> │ false ┆ false ┆ false │
#> │ null  ┆ null  ┆ null  │
#> │ null  ┆ null  ┆ true  │
#> └───────┴───────┴───────┘
df$with_columns(
  pl$any_horizontal("a", "b", "c")$alias("any")
)
#> shape: (4, 4)
#> ┌───────┬───────┬───────┬───────┐
#> │ a     ┆ b     ┆ c     ┆ any   │
#> │ ---   ┆ ---   ┆ ---   ┆ ---   │
#> │ bool  ┆ bool  ┆ bool  ┆ bool  │
#> ╞═══════╪═══════╪═══════╪═══════╡
#> │ false ┆ true  ┆ true  ┆ true  │
#> │ false ┆ false ┆ false ┆ false │
#> │ null  ┆ null  ┆ null  ┆ null  │
#> │ null  ┆ null  ┆ true  ┆ true  │
#> └───────┴───────┴───────┴───────┘
# drop rows that only have missing values == keep rows that have at least one
# non-missing value
df$filter(
  pl$any_horizontal(pl$all()$is_not_null())
)
#> shape: (3, 3)
#> ┌───────┬───────┬───────┐
#> │ a     ┆ b     ┆ c     │
#> │ ---   ┆ ---   ┆ ---   │
#> │ bool  ┆ bool  ┆ bool  │
#> ╞═══════╪═══════╪═══════╡
#> │ false ┆ true  ┆ true  │
#> │ false ┆ false ┆ false │
#> │ null  ┆ null  ┆ true  │
#> └───────┴───────┴───────┘