Skip to content

Select all columns matching the given names

Source code

Description

Select all columns matching the given names

Usage

cs__by_name(..., require_all = TRUE, expand_patterns = FALSE)

Arguments

\<dynamic-dots\> Column names to select.
require_all Whether to match all names (the default) or any of the names.
expand_patterns Whether to expand regex patterns (^…$) and wildcards (\*) in names. Default is FALSE (treat names as literals).

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 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)
#> ┌─────┬─────┐
#> │ baz ┆ foo │
#> │ --- ┆ --- │
#> │ f64 ┆ str │
#> ╞═════╪═════╡
#> │ 2.0 ┆ x   │
#> │ 5.5 ┆ y   │
#> └─────┴─────┘
# 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  │
#> └─────┴───────┘