Skip to contents

sample_dataset() draws n rows from a data frame. With a seed, the draw is reproducible and the session's random number stream is left as it was.

Usage

sample_dataset(dataset, n = NULL, seed = NULL, replace = FALSE)

Arguments

dataset

A data frame.

n

Number of rows to draw. When n is NULL or at least nrow(dataset) and replace = FALSE, the data is returned unchanged, in its original order.

seed

Random seed, or NULL (the default) to use the current random number stream.

replace

Whether to draw with replacement (default FALSE).

Value

A data frame with the drawn rows.

Examples

df <- data.frame(x = 1:10, y = letters[1:10])

sample_dataset(df, n = 3, seed = 42)
#>     x y
#> 1   1 a
#> 5   5 e
#> 10 10 j
identical(
  sample_dataset(df, n = 3, seed = 42),
  sample_dataset(df, n = 3, seed = 42)
)
#> [1] TRUE