Skip to content

Create an expression representing column(s) in a dataframe

Source code

Description

Create an expression representing column(s) in a dataframe

Usage

pl$col(...)

Arguments

One of the following:
  • character vectors
    • Single wildcard “\*“ has a special meaning: check the examples.
  • RPolarsDataTypes
  • a list of RPolarsDataTypes

Value

Expr of a column or columns

Examples

library(polars)

# a single column by a character
pl$col("foo")
#> polars Expr: col("foo")
# multiple columns by characters
pl$col("foo", "bar")
#> polars Expr: cols(["foo", "bar"])
# multiple columns by RPolarsDataTypes
pl$col(pl$Float64, pl$String)
#> polars Expr: dtype_columns([Float64, String])
# Single `"*"` is converted to a wildcard expression
pl$col("*")
#> polars Expr: *
# multiple character vectors and a list of RPolarsDataTypes are also allowed
pl$col(c("foo", "bar"), "baz")
#> polars Expr: cols(["foo", "bar", "baz"])
pl$col("foo", c("bar", "baz"))
#> polars Expr: cols(["foo", "bar", "baz"])
pl$col(list(pl$Float64, pl$String))
#> polars Expr: dtype_columns([Float64, String])
# there are some special notations for selecting columns
df = pl$DataFrame(foo = 1:3, bar = 4:6, baz = 7:9)

# select all columns with a wildcard `"*"`
df$select(pl$col("*"))
#> shape: (3, 3)
#> ┌─────┬─────┬─────┐
#> │ foo ┆ bar ┆ baz │
#> │ --- ┆ --- ┆ --- │
#> │ i32 ┆ i32 ┆ i32 │
#> ╞═════╪═════╪═════╡
#> │ 1   ┆ 4   ┆ 7   │
#> │ 2   ┆ 5   ┆ 8   │
#> │ 3   ┆ 6   ┆ 9   │
#> └─────┴─────┴─────┘
# select multiple columns by a regular expression
# starts with `^` and ends with `$`
df$select(pl$col(c("^ba.*$")))
#> shape: (3, 2)
#> ┌─────┬─────┐
#> │ bar ┆ baz │
#> │ --- ┆ --- │
#> │ i32 ┆ i32 │
#> ╞═════╪═════╡
#> │ 4   ┆ 7   │
#> │ 5   ┆ 8   │
#> │ 6   ┆ 9   │
#> └─────┴─────┘