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.
fraction Fraction of rows to return (between 0 and 1). Cannot be used with n.
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.3          ┆ 2.8         ┆ 5.1          ┆ 1.5         ┆ virginica  │
#> │ 5.5          ┆ 2.3         ┆ 4.0          ┆ 1.3         ┆ versicolor │
#> │ 6.9          ┆ 3.2         ┆ 5.7          ┆ 2.3         ┆ virginica  │
#> │ 6.5          ┆ 3.0         ┆ 5.5          ┆ 1.8         ┆ virginica  │
#> │ …            ┆ …           ┆ …            ┆ …           ┆ …          │
#> │ 5.2          ┆ 2.7         ┆ 3.9          ┆ 1.4         ┆ versicolor │
#> │ 4.9          ┆ 2.5         ┆ 4.5          ┆ 1.7         ┆ virginica  │
#> │ 4.8          ┆ 3.4         ┆ 1.6          ┆ 0.2         ┆ setosa     │
#> │ 5.7          ┆ 4.4         ┆ 1.5          ┆ 0.4         ┆ setosa     │
#> │ 4.3          ┆ 3.0         ┆ 1.1          ┆ 0.1         ┆ setosa     │
#> └──────────────┴─────────────┴──────────────┴─────────────┴────────────┘
df$sample(frac = 0.1)
#> shape: (15, 5)
#> ┌──────────────┬─────────────┬──────────────┬─────────────┬────────────┐
#> │ Sepal.Length ┆ Sepal.Width ┆ Petal.Length ┆ Petal.Width ┆ Species    │
#> │ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---        │
#> │ f64          ┆ f64         ┆ f64          ┆ f64         ┆ cat        │
#> ╞══════════════╪═════════════╪══════════════╪═════════════╪════════════╡
#> │ 7.7          ┆ 2.6         ┆ 6.9          ┆ 2.3         ┆ virginica  │
#> │ 5.1          ┆ 3.5         ┆ 1.4          ┆ 0.2         ┆ setosa     │
#> │ 5.7          ┆ 3.8         ┆ 1.7          ┆ 0.3         ┆ setosa     │
#> │ 5.6          ┆ 2.7         ┆ 4.2          ┆ 1.3         ┆ versicolor │
#> │ 5.6          ┆ 3.0         ┆ 4.1          ┆ 1.3         ┆ versicolor │
#> │ …            ┆ …           ┆ …            ┆ …           ┆ …          │
#> │ 6.5          ┆ 3.2         ┆ 5.1          ┆ 2.0         ┆ virginica  │
#> │ 5.7          ┆ 3.0         ┆ 4.2          ┆ 1.2         ┆ versicolor │
#> │ 5.1          ┆ 3.8         ┆ 1.9          ┆ 0.4         ┆ setosa     │
#> │ 6.2          ┆ 2.2         ┆ 4.5          ┆ 1.5         ┆ versicolor │
#> │ 6.8          ┆ 2.8         ┆ 4.8          ┆ 1.4         ┆ versicolor │
#> └──────────────┴─────────────┴──────────────┴─────────────┴────────────┘