Skip to content

Create a literal value

Source code

Description

Create a literal value

Usage

pl$lit(x)

Arguments

x A vector of any length

Details

pl$lit(NULL) translates into a polars null.

Value

Expr

Examples

library("polars")

# values to literal, explicit `pl$lit(42)` implicit `+ 2`
pl$col("some_column") / pl$lit(42) + 2
#> polars Expr: [([(col("some_column")) // (dyn float: 42.0)]) + (dyn float: 2.0)]
# vector to literal explicitly via Series and back again
# R vector to expression and back again
pl$select(pl$lit(as_polars_series(1:4)))$to_list()[[1L]]
#> [1] 1 2 3 4
# r vector to literal and back r vector
pl$lit(1:4)$to_r()
#> [1] 1 2 3 4
# r vector to literal to dataframe
pl$select(pl$lit(1:4))
#> shape: (4, 1)
#> ┌─────┐
#> │     │
#> │ --- │
#> │ i32 │
#> ╞═════╡
#> │ 1   │
#> │ 2   │
#> │ 3   │
#> │ 4   │
#> └─────┘
# r vector to literal to Series
pl$lit(1:4)$to_series()
#> polars Series: shape: (4,)
#> Series: '' [i32]
#> [
#>  1
#>  2
#>  3
#>  4
#> ]
# vectors to literal implicitly
(pl$lit(2) + 1:4) / 4:1
#> polars Expr: [([(dyn float: 2.0) + (Series)]) // (Series)]