Skip to content

Select all columns having names consisting only of digits

Description

Select all columns having names consisting only of digits

Usage

cs__digit(ascii_only = FALSE)

Arguments

ascii_only Indicate whether to consider only ASCII alphabetic characters, or the full Unicode range of valid letters (accented, idiographic, etc).

Details

Matching column names cannot contain any non-digit characters. Note that the definition of "digit" consists of all valid Unicode digit characters (d) by default; this can be changed by setting ascii_only = TRUE.

Value

A Polars selector

See Also

cs for the documentation on operators supported by Polars selectors.

Examples

library("polars")

df <- pl$DataFrame(
  key = c("aaa", "bbb"),
  `2001` = 1:2,
  `2025` = 3:4
)

# Select columns with digit names:
df$select(cs$digit())
#> shape: (2, 2)
#> ┌──────┬──────┐
#> │ 2001 ┆ 2025 │
#> │ ---  ┆ ---  │
#> │ i32  ┆ i32  │
#> ╞══════╪══════╡
#> │ 1    ┆ 3    │
#> │ 2    ┆ 4    │
#> └──────┴──────┘
# Select all columns except for those with digit names:
df$select(!cs$digit())
#> shape: (2, 1)
#> ┌─────┐
#> │ key │
#> │ --- │
#> │ str │
#> ╞═════╡
#> │ aaa │
#> │ bbb │
#> └─────┘
# Demonstrate use of ascii_only flag (by default all valid unicode digits
# are considered, but this can be constrained to ascii 0-9):
df <- pl$DataFrame(`१९९९` = 1999, `२०७७` = 2077, `3000` = 3000)
df$select(cs$digit())
#> shape: (1, 3)
#> ┌────────┬────────┬────────┐
#> │ १९९९   ┆ २०७७   ┆ 3000   │
#> │ ---    ┆ ---    ┆ ---    │
#> │ f64    ┆ f64    ┆ f64    │
#> ╞════════╪════════╪════════╡
#> │ 1999.0 ┆ 2077.0 ┆ 3000.0 │
#> └────────┴────────┴────────┘
df$select(cs$digit(ascii_only = TRUE))
#> shape: (1, 1)
#> ┌────────┐
#> │ 3000   │
#> │ ---    │
#> │ f64    │
#> ╞════════╡
#> │ 3000.0 │
#> └────────┘