step_sample: Sample rows using dplyr

Description Usage Arguments Value Examples

View source: R/sample.R

Description

step_sample creates a specification of a recipe step that will sample rows using dplyr::sample_n() or dplyr::sample_frac().

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
step_sample(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  size = NULL,
  replace = FALSE,
  skip = TRUE,
  id = rand_id("sample")
)

## S3 method for class 'step_sample'
tidy(x, ...)

Arguments

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. For the tidy method, these are not currently used.

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), 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 NULL uses dplyr::sample_n() with the size of the training set (or smaller for smaller new_data).

replace

Sample with or without replacement?

skip

A logical. Should the step be skipped when the recipe is baked by bake.recipe()? While all operations are baked when prep.recipe() 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; in most instances that affect the rows of the data being predicted, this step probably should not be applied.

id

A character string that is unique to this step to identify it.

x

A step_sample object

Value

An updated version of recipe with the new step added to the sequence of existing steps (if any). For the tidy method, a tibble with columns size, replace, and id.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Uses `sample_n`
recipe( ~ ., data = iris) %>%
  step_sample(size = 1) %>%
  prep(training = iris) %>%
  juice() %>%
  nrow()

# Uses `sample_frac`
recipe( ~ ., data = iris) %>%
  step_sample(size = 0.9999) %>%
  prep(training = iris) %>%
  juice() %>%
  nrow()

# Uses `sample_n` and returns _at maximum_ 120 samples.
smaller_iris <-
  recipe( ~ ., data = iris) %>%
  step_sample() %>%
  prep(training = iris %>% slice(1:120))

juice(smaller_iris) %>% nrow()
bake(smaller_iris, iris %>% slice(121:150)) %>% nrow()

recipes documentation built on July 2, 2020, 4:02 a.m.