smpl {xpectr}R Documentation

Random sampling

Description

[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)

[Package xpectr version 0.4.3 Index]