step_sample | R Documentation |
step_sample()
creates a specification of a recipe step that will sample
rows using dplyr::sample_n()
or dplyr::sample_frac()
.
step_sample(
recipe,
...,
role = NA,
trained = FALSE,
size = NULL,
replace = FALSE,
skip = TRUE,
id = rand_id("sample")
)
recipe |
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. |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
size |
An integer or fraction. If the value is within (0, 1),
|
replace |
Sample with or without replacement? |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
id |
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 recipe()
.
When you tidy()
this step, a tibble with columns
size
, replace
, and id
is returned.
This step performs an unsupervised operation that can utilize case weights.
As a result, case weights are only used with frequency weights. For more
information, see the documentation in case_weights and the examples on
tidymodels.org
.
Other row operation steps:
step_arrange()
,
step_filter()
,
step_impute_roll()
,
step_lag()
,
step_naomit()
,
step_shuffle()
,
step_slice()
Other dplyr steps:
step_arrange()
,
step_filter()
,
step_mutate_at()
,
step_mutate()
,
step_rename_at()
,
step_rename()
,
step_select()
,
step_slice()
# Uses `sample_n`
recipe(~., data = mtcars) %>%
step_sample(size = 1) %>%
prep(training = mtcars) %>%
bake(new_data = NULL) %>%
nrow()
# Uses `sample_frac`
recipe(~., data = mtcars) %>%
step_sample(size = 0.9999) %>%
prep(training = mtcars) %>%
bake(new_data = NULL) %>%
nrow()
# 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()
bake(smaller_cars, new_data = mtcars %>% slice(21:32)) %>% nrow()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.