Skip to content

Select columns that start with the given substring(s)

Description

Select columns that start with the given substring(s)

Usage

cs__starts_with(...)

Arguments

\<dynamic-dots\> Substring(s) that matching column names should end with.

Value

A Polars selector

See Also

cs for the documentation on operators supported by Polars selectors.

Examples

library("polars")

df <- pl$DataFrame(
  foo = c("x", "y"),
  bar = c(123, 456),
  baz = c(2.0, 5.5),
  zap = c(FALSE, TRUE)
)

# Select columns that start with the substring "b":
df$select(cs$starts_with("b"))
#> shape: (2, 2)
#> ┌───────┬─────┐
#> │ bar   ┆ baz │
#> │ ---   ┆ --- │
#> │ f64   ┆ f64 │
#> ╞═══════╪═════╡
#> │ 123.0 ┆ 2.0 │
#> │ 456.0 ┆ 5.5 │
#> └───────┴─────┘
# Select columns that start with either the letter "b" or "z":
df$select(cs$starts_with("b", "z"))
#> shape: (2, 3)
#> ┌───────┬─────┬───────┐
#> │ bar   ┆ baz ┆ zap   │
#> │ ---   ┆ --- ┆ ---   │
#> │ f64   ┆ f64 ┆ bool  │
#> ╞═══════╪═════╪═══════╡
#> │ 123.0 ┆ 2.0 ┆ false │
#> │ 456.0 ┆ 5.5 ┆ true  │
#> └───────┴─────┴───────┘
# Select all columns except for those that start with the substring "b":
df$select(!cs$starts_with("b"))
#> shape: (2, 2)
#> ┌─────┬───────┐
#> │ foo ┆ zap   │
#> │ --- ┆ ---   │
#> │ str ┆ bool  │
#> ╞═════╪═══════╡
#> │ x   ┆ false │
#> │ y   ┆ true  │
#> └─────┴───────┘