Get the value by index in every sub-array
Description
This allows to extract one value per array only. Values are 0-indexed
(so index 0
would return the first item of every sub-array)
and negative values start from the end (so index -1
returns
the last item).
Usage
<Expr>$arr$get(index, ..., null_on_oob = TRUE)
Arguments
index
|
An Expr or something coercible to an Expr, that must return a single index. |
…
|
These dots are for future extensions and must be empty. |
null_on_oob
|
If TRUE , return null if an index is out of
bounds. Otherwise, raise an error.
|
Value
A polars expression
Examples
library("polars")
df <- pl$DataFrame(
values = list(c(1, 2), c(3, 4), c(NA, 6)),
idx = c(1, NA, 3)
)$cast(values = pl$Array(pl$Float64, 2))
df$with_columns(
using_expr = pl$col("values")$arr$get("idx"),
val_0 = pl$col("values")$arr$get(0),
val_minus_1 = pl$col("values")$arr$get(-1),
val_oob = pl$col("values")$arr$get(10)
)
#> shape: (3, 6)
#> ┌───────────────┬──────┬────────────┬───────┬─────────────┬─────────┐
#> │ values ┆ idx ┆ using_expr ┆ val_0 ┆ val_minus_1 ┆ val_oob │
#> │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
#> │ array[f64, 2] ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
#> ╞═══════════════╪══════╪════════════╪═══════╪═════════════╪═════════╡
#> │ [1.0, 2.0] ┆ 1.0 ┆ 2.0 ┆ 1.0 ┆ 2.0 ┆ null │
#> │ [3.0, 4.0] ┆ null ┆ null ┆ 3.0 ┆ 4.0 ┆ null │
#> │ [null, 6.0] ┆ 3.0 ┆ null ┆ null ┆ 6.0 ┆ null │
#> └───────────────┴──────┴────────────┴───────┴─────────────┴─────────┘