reproducibleSample: Reproducible Call to the Sample Function

View source: R/reproducibleSample.R

reproducibleSampleR Documentation

Reproducible Call to the Sample Function

Description

This function calls sample and stores the values that are required to reproduce the exact same sampling in the attribute random_seed of the returned vector. When this attribute is passed to another call of this function, the values returned will be the same as in the first call.

Usage

reproducibleSample(..., FUN = sample, random_seed = NULL)

Arguments

...

arguments passed to FUN.

FUN

the sample function to be called. Default: sample.

random_seed

vector of integer as stored in .Random.seed.

Value

This function returns what sample returns with an attribute random_seed attached.

Examples

# Take a sample
x <- reproducibleSample(1:100, 10)
x

# The full seed vector is returned in the attribute "random_seed"
random_seed <- attr(x, "random_seed")

# Take a new sample, this time passing the seed vector
y <- reproducibleSample(1:100, 10, random_seed = random_seed)
y

# The values are identical to the values of the first sampling
identical(x, y)


KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.