Learn R Programming

LSTbook (version 0.6)

take_sample: Samples from various kinds of objects

Description

A set of methods to generate random samples from data frames and data simulations. For data frames, individual rows are sampled. For vectors, elements are sampled.

Usage

take_sample(x, n, replace = FALSE, ...)

# S3 method for default take_sample( x, n = length(x), replace = FALSE, prob = NULL, .by = NULL, groups = .by, orig.ids = FALSE, ... )

resample(..., replace = TRUE)

Value

A vector or a data frame depending on the nature of the x argument.

Arguments

x

The object from which to sample

n

Size of the sample.

replace

Logical flag: whether to sample with replacement. (default: FALSE)

prob

Probabilities to use for sampling, one for each element of x

.by

Variables to use to define groups for sampling, as in {dplyr}. The sample size applies to each group.

groups

Variable indicating blocks to sample within

orig.ids

Logical. If TRUE, append a column named "orig.ids" with the row from the original x that the same came from.

...

Arguments to pass along to specific sample methods.

Details

These are based in spirit on the sample functions in the {mosaic} package, but are redefined here to 1) avoid a dependency on {mosaic} and 2) bring the arguments in line with the .by = features of {dplyr}.

Examples

Run this code
take_sample(sim_03, n=5) # run a simulation
take_sample(Clock_auction, n = 3) # from a data frame
take_sample(1:6, n = 6) # sample from a vector

Run the code above in your browser using DataLab