Skip to content

Select all columns with alphabetic names (e.g. only letters)

Source code

Description

Select all columns with alphabetic names (e.g. only letters)

Usage

cs__alpha(ascii_only = FALSE, ..., ignore_spaces = FALSE)

Arguments

ascii_only Indicate whether to consider only ASCII alphabetic characters, or the full Unicode range of valid letters (accented, idiographic, etc).
… These dots are for future extensions and must be empty.
ignore_spaces Indicate whether to ignore the presence of spaces in column names; if so, only the other (non-space) characters are considered.

Details

Matching column names cannot contain any non-alphabetic characters. Note that the definition of “alphabetic” consists of all valid Unicode alphabetic characters (p{Alphabetic}) 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 selectors.

Examples

library("polars")

df <- pl$DataFrame(
  no1 = c(100, 200, 300),
  café = c("espresso", "latte", "mocha"),
  `t or f` = c(TRUE, FALSE, NA),
  hmm = c("aaa", "bbb", "ccc"),
  都市 = c("東京", "大阪", "京都")
)

# Select columns with alphabetic names; note that accented characters and
# kanji are recognised as alphabetic here:
df$select(cs$alpha())
#> shape: (3, 3)
#> ┌──────────┬─────┬──────┐
#> │ café     ┆ hmm ┆ 都市 │
#> │ ---      ┆ --- ┆ ---  │
#> │ str      ┆ str ┆ str  │
#> ╞══════════╪═════╪══════╡
#> │ espresso ┆ aaa ┆ 東京 │
#> │ latte    ┆ bbb ┆ 大阪 │
#> │ mocha    ┆ ccc ┆ 京都 │
#> └──────────┴─────┴──────┘
# Constrain the definition of “alphabetic” to ASCII characters only:
df$select(cs$alpha(ascii_only = TRUE))
#> shape: (3, 1)
#> ┌─────┐
#> │ hmm │
#> │ --- │
#> │ str │
#> ╞═════╡
#> │ aaa │
#> │ bbb │
#> │ ccc │
#> └─────┘
df$select(cs$alpha(ascii_only = TRUE, ignore_spaces = TRUE))
#> shape: (3, 2)
#> ┌────────┬─────┐
#> │ t or f ┆ hmm │
#> │ ---    ┆ --- │
#> │ bool   ┆ str │
#> ╞════════╪═════╡
#> │ true   ┆ aaa │
#> │ false  ┆ bbb │
#> │ null   ┆ ccc │
#> └────────┴─────┘
# Select all columns except for those with alphabetic names:
df$select(!cs$alpha())
#> shape: (3, 2)
#> ┌───────┬────────┐
#> │ no1   ┆ t or f │
#> │ ---   ┆ ---    │
#> │ f64   ┆ bool   │
#> ╞═══════╪════════╡
#> │ 100.0 ┆ true   │
#> │ 200.0 ┆ false  │
#> │ 300.0 ┆ null   │
#> └───────┴────────┘
df$select(!cs$alpha(ignore_spaces = TRUE))
#> shape: (3, 1)
#> ┌───────┐
#> │ no1   │
#> │ ---   │
#> │ f64   │
#> ╞═══════╡
#> │ 100.0 │
#> │ 200.0 │
#> │ 300.0 │
#> └───────┘