Skip to content

Take a sample of rows from a DataFrame

Source code

Description

Take a sample of rows from a DataFrame

Usage

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

Arguments

n Number of rows to return. Cannot be used with fraction.
Ignored.
fraction Fraction of rows to return. Cannot be used with n. Can be larger than 1 if with_replacement is TRUE.
with_replacement Allow values to be sampled more than once.
shuffle If TRUE, the order of the sampled rows will be shuffled. If FALSE (default), the order of the returned rows will be neither stable nor fully random.
seed Seed for the random number generator. If set to NULL (default), a random seed is generated for each sample operation.

Value

DataFrame

Examples

library(polars)

df = pl$DataFrame(iris)
df$sample(n = 20)
#> shape: (20, 5)
#> ┌──────────────┬─────────────┬──────────────┬─────────────┬────────────┐
#> │ Sepal.Length ┆ Sepal.Width ┆ Petal.Length ┆ Petal.Width ┆ Species    │
#> │ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---        │
#> │ f64          ┆ f64         ┆ f64          ┆ f64         ┆ cat        │
#> ╞══════════════╪═════════════╪══════════════╪═════════════╪════════════╡
#> │ 5.2          ┆ 3.5         ┆ 1.5          ┆ 0.2         ┆ setosa     │
#> │ 6.0          ┆ 3.0         ┆ 4.8          ┆ 1.8         ┆ virginica  │
#> │ 5.3          ┆ 3.7         ┆ 1.5          ┆ 0.2         ┆ setosa     │
#> │ 5.1          ┆ 2.5         ┆ 3.0          ┆ 1.1         ┆ versicolor │
#> │ 4.7          ┆ 3.2         ┆ 1.3          ┆ 0.2         ┆ setosa     │
#> │ …            ┆ …           ┆ …            ┆ …           ┆ …          │
#> │ 5.6          ┆ 2.8         ┆ 4.9          ┆ 2.0         ┆ virginica  │
#> │ 6.3          ┆ 2.8         ┆ 5.1          ┆ 1.5         ┆ virginica  │
#> │ 4.8          ┆ 3.0         ┆ 1.4          ┆ 0.1         ┆ setosa     │
#> │ 5.8          ┆ 2.7         ┆ 5.1          ┆ 1.9         ┆ virginica  │
#> │ 6.0          ┆ 3.4         ┆ 4.5          ┆ 1.6         ┆ versicolor │
#> └──────────────┴─────────────┴──────────────┴─────────────┴────────────┘
df$sample(fraction = 0.1)
#> shape: (15, 5)
#> ┌──────────────┬─────────────┬──────────────┬─────────────┬────────────┐
#> │ Sepal.Length ┆ Sepal.Width ┆ Petal.Length ┆ Petal.Width ┆ Species    │
#> │ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---        │
#> │ f64          ┆ f64         ┆ f64          ┆ f64         ┆ cat        │
#> ╞══════════════╪═════════════╪══════════════╪═════════════╪════════════╡
#> │ 5.0          ┆ 2.3         ┆ 3.3          ┆ 1.0         ┆ versicolor │
#> │ 4.4          ┆ 3.0         ┆ 1.3          ┆ 0.2         ┆ setosa     │
#> │ 5.6          ┆ 2.8         ┆ 4.9          ┆ 2.0         ┆ virginica  │
#> │ 6.0          ┆ 2.7         ┆ 5.1          ┆ 1.6         ┆ versicolor │
#> │ 5.0          ┆ 3.4         ┆ 1.6          ┆ 0.4         ┆ setosa     │
#> │ …            ┆ …           ┆ …            ┆ …           ┆ …          │
#> │ 5.7          ┆ 2.8         ┆ 4.5          ┆ 1.3         ┆ versicolor │
#> │ 5.2          ┆ 3.4         ┆ 1.4          ┆ 0.2         ┆ setosa     │
#> │ 6.5          ┆ 3.2         ┆ 5.1          ┆ 2.0         ┆ virginica  │
#> │ 5.4          ┆ 3.9         ┆ 1.7          ┆ 0.4         ┆ setosa     │
#> │ 5.6          ┆ 2.7         ┆ 4.2          ┆ 1.3         ┆ versicolor │
#> └──────────────┴─────────────┴──────────────┴─────────────┴────────────┘