Sample rows using dplyrSource:
step_sample( recipe, ..., role = NA, trained = FALSE, size = NULL, replace = FALSE, skip = TRUE, id = rand_id("sample") )
A recipe object. The step will be added to the sequence of operations for this recipe.
Argument ignored; included for consistency with other step specification functions.
Not used by this step since no new variables are created.
A logical to indicate if the quantities for preprocessing have been estimated.
An integer or fraction. If the value is within (0, 1),
dplyr::sample_frac()is applied to the data. If an integer value of 1 or greater is used,
dplyr::sample_n()is applied. The default of
dplyr::sample_n()with the size of the training set (or smaller for smaller
Sample with or without replacement?
A logical. Should the step be skipped when the recipe is baked by
bake()? While all operations are baked when
prep()is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using
skip = FALSE.
A character string that is unique to this step to identify it.
An updated version of
recipe with the new step added to the
sequence of any existing operations.
This step can entirely remove observations (rows of data), which can have
unintended and/or problematic consequences when applying the step to new
data later via
bake(). Consider whether
skip = TRUE or
skip = FALSE is more appropriate in any given use case. In most instances
that affect the rows of the data being predicted, this step probably should
not be applied at all; instead, execute operations like this outside and
before starting a preprocessing
tidy() this step, a tibble with columns
id is returned.
# Uses `sample_n` recipe( ~ ., data = mtcars) %>% step_sample(size = 1) %>% prep(training = mtcars) %>% bake(new_data = NULL) %>% nrow() #>  1 # Uses `sample_frac` recipe( ~ ., data = mtcars) %>% step_sample(size = 0.9999) %>% prep(training = mtcars) %>% bake(new_data = NULL) %>% nrow() #>  32 # Uses `sample_n` and returns _at maximum_ 20 samples. smaller_cars <- recipe( ~ ., data = mtcars) %>% step_sample() %>% prep(training = mtcars %>% slice(1:20)) bake(smaller_cars, new_data = NULL) %>% nrow() #>  20 bake(smaller_cars, new_data = mtcars %>% slice(21:32)) %>% nrow() #>  12