smpl: Random sampling

View source: R/sample.R

smplR Documentation

Random sampling

Description

\Sexpr[results=rd, stage=render]{lifecycle::badge("experimental")}

Samples a vector, factor or data.frame. Useful to reduce size of testthat expect_* tests. Not intended for other purposes.

Wraps sample.int(). data.frames are sampled row-wise.

The seed is set within the function with sample.kind as "Rounding" for compatibility with R versions < 3.6.0. On exit, the random state is restored.

Usage

smpl(data, n, keep_order = TRUE, seed = 42)

Arguments

data

vector or data.frame. (Logical)

n

Number of elements/rows to sample.

N.B. No replacement is used, why n > the number of elements/rows in `data` won't perform oversampling.

keep_order

Whether to keep the order of the elements. (Logical)

seed

seed to use.

The seed is set with sample.kind = "Rounding" for compatibility with R versions < 3.6.0.

Value

When `data` has <=`n` elements, `data` is returned. Otherwise, `data` is sampled and returned.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

Examples

# Attach packages
library(xpectr)

smpl(c(1,2,3,4,5), n = 3)
smpl(data.frame("a" = c(1,2,3,4,5), "b" = c(2,3,4,5,6), stringsAsFactors = FALSE), n = 3)

xpectr documentation built on Nov. 18, 2022, 5:10 p.m.