Run any polars expression on the sub-array’s values
Description
Run any polars expression on the sub-array’s values
Usage
<Expr>$arr$eval(expr, ..., as_list = FALSE)
Arguments
expr
|
Expression to run. Note that you can select an element with
pl$element(), pl$first(), and more. See
Examples.
|
…
|
These dots are for future extensions and must be empty. |
as_list
|
Collect the resulting data into a list datatype (instead of array datatype). This allows for expressions which output a variable amount of data. |
Value
A polars expression
Examples
library("polars")
df <- pl$DataFrame(
a = list(c(1, 1), c(8, 5), c(3, 2))
)$cast(pl$Array(pl$Float64, 2))
df$with_columns(
cum_sum = pl$col("a")$arr$eval(pl$element()$cum_sum())
)
#> shape: (3, 2)
#> ┌───────────────┬───────────────┐
#> │ a ┆ cum_sum │
#> │ --- ┆ --- │
#> │ array[f64, 2] ┆ array[f64, 2] │
#> ╞═══════════════╪═══════════════╡
#> │ [1.0, 1.0] ┆ [1.0, 2.0] │
#> │ [8.0, 5.0] ┆ [8.0, 13.0] │
#> │ [3.0, 2.0] ┆ [3.0, 5.0] │
#> └───────────────┴───────────────┘
# This would error without `as_list = TRUE` since `$unique()` doesn't return
# the same number of values in each row.
df$with_columns(
cum_sum = pl$col("a")$arr$eval(pl$element()$unique(), as_list = TRUE)
)
#> shape: (3, 2)
#> ┌───────────────┬────────────┐
#> │ a ┆ cum_sum │
#> │ --- ┆ --- │
#> │ array[f64, 2] ┆ list[f64] │
#> ╞═══════════════╪════════════╡
#> │ [1.0, 1.0] ┆ [1.0] │
#> │ [8.0, 5.0] ┆ [8.0, 5.0] │
#> │ [3.0, 2.0] ┆ [3.0, 2.0] │
#> └───────────────┴────────────┘