Sample values from every sub-list
Description
Sample values from every sub-list
Usage
<Expr>$list$sample(
n = NULL,
...,
fraction = NULL,
with_replacement = FALSE,
shuffle = FALSE,
seed = NULL
)
Arguments
n
|
Number of items to return. Cannot be used with fraction.
Defaults to 1 if fraction is NULL .
|
…
|
These dots are for future extensions and must be empty. |
fraction
|
Fraction of items to return. Cannot be used with n .
|
with_replacement
|
Allow values to be sampled more than once. |
shuffle
|
Shuffle the order of sampled data points. |
seed
|
Seed for the random number generator. If NULL (default), a
random seed is generated for each sample operation.
|
Value
A polars expression
Examples
library("polars")
df <- pl$DataFrame(
values = list(1:3, NA, c(NA, 3L), 5:7),
n = c(1, 1, 1, 2)
)
df$with_columns(
sample = pl$col("values")$list$sample(n = pl$col("n"), seed = 1)
)
#> shape: (4, 3)
#> ┌───────────┬─────┬───────────┐
#> │ values ┆ n ┆ sample │
#> │ --- ┆ --- ┆ --- │
#> │ list[i32] ┆ f64 ┆ list[i32] │
#> ╞═══════════╪═════╪═══════════╡
#> │ [1, 2, 3] ┆ 1.0 ┆ [3] │
#> │ [null] ┆ 1.0 ┆ [null] │
#> │ [null, 3] ┆ 1.0 ┆ [3] │
#> │ [5, 6, 7] ┆ 2.0 ┆ [6, 5] │
#> └───────────┴─────┴───────────┘