Skip to content

Select all String (and, optionally, Categorical) string columns.

Description

Select all String (and, optionally, Categorical) string columns.

Usage

cs__string(..., include_categorical = FALSE)

Arguments

These dots are for future extensions and must be empty.
include_categorical If TRUE, also select categorical columns.

Value

A Polars selector

See Also

cs for the documentation on operators supported by Polars selectors.

Examples

library("polars")

df <- pl$DataFrame(
  w = c("xx", "yy", "xx", "yy", "xx"),
  x = c(1, 2, 1, 4, -2),
  y = c(3.0, 4.5, 1.0, 2.5, -2.0),
  z = c("a", "b", "a", "b", "b")
)$with_columns(
  z = pl$col("z")$cast(pl$Categorical())
)

# Group by all string columns, sum the numeric columns, then sort by the
# string cols:
df$group_by(cs$string())$agg(cs$numeric()$sum())$sort(cs$string())
#> shape: (2, 3)
#> ┌─────┬─────┬─────┐
#> │ w   ┆ x   ┆ y   │
#> │ --- ┆ --- ┆ --- │
#> │ str ┆ f64 ┆ f64 │
#> ╞═════╪═════╪═════╡
#> │ xx  ┆ 0.0 ┆ 2.0 │
#> │ yy  ┆ 6.0 ┆ 7.0 │
#> └─────┴─────┴─────┘
# Group by all string and categorical columns:
df$
  group_by(cs$string(include_categorical = TRUE))$
  agg(cs$numeric()$sum())$
  sort(cs$string(include_categorical = TRUE))
#> shape: (3, 4)
#> ┌─────┬─────┬──────┬──────┐
#> │ w   ┆ z   ┆ x    ┆ y    │
#> │ --- ┆ --- ┆ ---  ┆ ---  │
#> │ str ┆ cat ┆ f64  ┆ f64  │
#> ╞═════╪═════╪══════╪══════╡
#> │ xx  ┆ a   ┆ 2.0  ┆ 4.0  │
#> │ xx  ┆ b   ┆ -2.0 ┆ -2.0 │
#> │ yy  ┆ b   ┆ 6.0  ┆ 7.0  │
#> └─────┴─────┴──────┴──────┘