Skip to contents

Restores the tbl_sample class on a data frame that carries sampling attributes (design, stages_executed) but has lost its class, for example after a tidyr operation such as tidyr::uncount().

Usage

as_tbl_sample(x, ...)

# S3 method for class 'tbl_sample'
as_tbl_sample(x, ...)

# S3 method for class 'data.frame'
as_tbl_sample(x, ...)

Arguments

x

A data frame with sampling attributes.

...

Not used.

Value

A tbl_sample, or an error if x does not carry the required attributes.

Class preservation

All dplyr verbs (mutate, filter, select, *_join, etc.) preserve the tbl_sample class automatically.

Some operations strip the class but keep the sampling attributes. Use as_tbl_sample() to restore it:

Other operations strip both class and attributes and are not recoverable. Use dplyr alternatives instead:

Examples

design <- sampling_design() |>
  stratify_by(region) |>
  draw(n = 20)

sample <- execute(design, bfa_eas, seed = 42)

# as_tibble() strips the class but keeps attributes
plain <- tibble::as_tibble(sample)
is_tbl_sample(plain)
#> [1] FALSE

# as_tbl_sample() restores it
restored <- as_tbl_sample(plain)
is_tbl_sample(restored)
#> [1] TRUE