sample {LSTbook} | R Documentation |
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
sample(x, n, replace = FALSE, ...)
## Default S3 method:
sample(
x,
n = length(x),
replace = FALSE,
prob = NULL,
.by = NULL,
groups = .by,
orig.ids = FALSE,
...
)
resample(..., replace = TRUE)
Arguments
x |
The object from which to sample |
n |
Size of the sample. |
replace |
Logical flag: whether to sample with replacement. (default: |
prob |
Probabilities to use for sampling, one for each element of |
.by |
Variables to use to define groups for sampling, as in |
groups |
Variable indicating blocks to sample within |
orig.ids |
Logical. If |
... |
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}
.
Value
A vector or a data frame depending on the nature of the x
argument.
Examples
sample(sim_03, n=5) # run a simulation
sample(Clock_auction, n = 3) # from a data frame
sample(1:6, n = 6) # sample from a vector