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 = as_polars_df(iris)
df$sample(n = 20)
#> shape: (20, 5)
#> ┌──────────────┬─────────────┬──────────────┬─────────────┬────────────┐
#> │ Sepal.Length ┆ Sepal.Width ┆ Petal.Length ┆ Petal.Width ┆ Species    │
#> │ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---        │
#> │ f64          ┆ f64         ┆ f64          ┆ f64         ┆ cat        │
#> ╞══════════════╪═════════════╪══════════════╪═════════════╪════════════╡
#> │ 6.3          ┆ 2.7         ┆ 4.9          ┆ 1.8         ┆ virginica  │
#> │ 5.5          ┆ 2.6         ┆ 4.4          ┆ 1.2         ┆ versicolor │
#> │ 4.9          ┆ 3.0         ┆ 1.4          ┆ 0.2         ┆ setosa     │
#> │ 5.7          ┆ 3.8         ┆ 1.7          ┆ 0.3         ┆ setosa     │
#> │ 5.7          ┆ 2.5         ┆ 5.0          ┆ 2.0         ┆ virginica  │
#> │ …            ┆ …           ┆ …            ┆ …           ┆ …          │
#> │ 5.5          ┆ 2.4         ┆ 3.7          ┆ 1.0         ┆ versicolor │
#> │ 6.8          ┆ 3.2         ┆ 5.9          ┆ 2.3         ┆ virginica  │
#> │ 4.9          ┆ 2.4         ┆ 3.3          ┆ 1.0         ┆ versicolor │
#> │ 4.4          ┆ 3.2         ┆ 1.3          ┆ 0.2         ┆ setosa     │
#> │ 5.5          ┆ 2.4         ┆ 3.8          ┆ 1.1         ┆ versicolor │
#> └──────────────┴─────────────┴──────────────┴─────────────┴────────────┘
df$sample(fraction = 0.1)
#> shape: (15, 5)
#> ┌──────────────┬─────────────┬──────────────┬─────────────┬────────────┐
#> │ Sepal.Length ┆ Sepal.Width ┆ Petal.Length ┆ Petal.Width ┆ Species    │
#> │ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---        │
#> │ f64          ┆ f64         ┆ f64          ┆ f64         ┆ cat        │
#> ╞══════════════╪═════════════╪══════════════╪═════════════╪════════════╡
#> │ 6.1          ┆ 2.8         ┆ 4.7          ┆ 1.2         ┆ versicolor │
#> │ 5.8          ┆ 2.7         ┆ 5.1          ┆ 1.9         ┆ virginica  │
#> │ 4.6          ┆ 3.1         ┆ 1.5          ┆ 0.2         ┆ setosa     │
#> │ 6.4          ┆ 2.8         ┆ 5.6          ┆ 2.1         ┆ virginica  │
#> │ 5.1          ┆ 3.8         ┆ 1.6          ┆ 0.2         ┆ setosa     │
#> │ …            ┆ …           ┆ …            ┆ …           ┆ …          │
#> │ 5.2          ┆ 2.7         ┆ 3.9          ┆ 1.4         ┆ versicolor │
#> │ 5.0          ┆ 3.0         ┆ 1.6          ┆ 0.2         ┆ setosa     │
#> │ 5.4          ┆ 3.7         ┆ 1.5          ┆ 0.2         ┆ setosa     │
#> │ 7.4          ┆ 2.8         ┆ 6.1          ┆ 1.9         ┆ virginica  │
#> │ 5.0          ┆ 2.0         ┆ 3.5          ┆ 1.0         ┆ versicolor │
#> └──────────────┴─────────────┴──────────────┴─────────────┴────────────┘