Skip to content

Sample from this expression

Source code

Description

Sample from this expression

Usage

<Expr>$sample(
  n = NULL,
  ...,
  fraction = NULL,
  with_replacement = FALSE,
  shuffle = NULL,
  seed = NULL
)

Arguments

n Number of items to return. Can be an Expr. Strings are parsed as column names. 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. Can be an Expr. Strings are parsed as column names. Cannot be used with n.
with_replacement Allow values to be sampled more than once.
shuffle If TRUE, explicitly shuffle the sampled data points. If FALSE, maintain their relative order. If NULL (default), use a high-performance algorithm without guaranteeing an order.
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(a = 1:3)
df$select(pl$col("a")$sample(
  fraction = 1, with_replacement = TRUE, seed = 1
))
#> shape: (3, 1)
#> ┌─────┐
#> │ a   │
#> │ --- │
#> │ i32 │
#> ╞═════╡
#> │ 3   │
#> │ 3   │
#> │ 1   │
#> └─────┘