Skip to content

Select all columns matching the given names

Description

Select all columns matching the given names

Usage

cs__by_name(..., require_all = TRUE)

Arguments

\<dynamic-dots\> Column names to select.
require_all Whether to match all names (the default) or any of the names.

Details

Matching columns are returned in the order in which their indexes appear in the selector, not the underlying schema order.

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 by name:
df$select(cs$by_name("foo", "bar"))
#> shape: (2, 2)
#> ┌─────┬───────┐
#> │ foo ┆ bar   │
#> │ --- ┆ ---   │
#> │ str ┆ f64   │
#> ╞═════╪═══════╡
#> │ x   ┆ 123.0 │
#> │ y   ┆ 456.0 │
#> └─────┴───────┘
# Match any of the given columns by name:
df$select(cs$by_name("baz", "moose", "foo", "bear", require_all = FALSE))
#> shape: (2, 2)
#> ┌─────┬─────┐
#> │ foo ┆ baz │
#> │ --- ┆ --- │
#> │ str ┆ f64 │
#> ╞═════╪═════╡
#> │ x   ┆ 2.0 │
#> │ y   ┆ 5.5 │
#> └─────┴─────┘
# Match all columns except for those given:
df$select(!cs$by_name("foo", "bar"))
#> shape: (2, 2)
#> ┌─────┬───────┐
#> │ baz ┆ zap   │
#> │ --- ┆ ---   │
#> │ f64 ┆ bool  │
#> ╞═════╪═══════╡
#> │ 2.0 ┆ false │
#> │ 5.5 ┆ true  │
#> └─────┴───────┘