Learn R Programming

mosaic (version 0.4-1)

resample: More Random Samples

Description

These functions simplify and unify sampling in various ways.

Usage

resample(x, size, replace = TRUE, prob = NULL,
    groups = NULL, orig.ids = FALSE, ...)

deal(x, size, replace = FALSE, prob = NULL, groups = NULL, orig.ids = FALSE)

shuffle(x, replace = FALSE, prob = NULL, groups = NULL, orig.ids = FALSE)

sample(x, size, replace = FALSE, ...)

## S3 method for class 'default': sample(x, size, replace = FALSE, prob = NULL, groups = NULL, orig.ids = FALSE, ...)

## S3 method for class 'data.frame': sample(x, size, replace = FALSE, prob = NULL, groups = NULL, orig.ids = TRUE, fixed = names(x), shuffled = c(), invisibly.return = NULL, ...)

## S3 method for class 'matrix': sample(x, size, replace = FALSE, prob = NULL, groups = NULL, orig.ids = FALSE, ...)

## S3 method for class 'factor': sample(x, size, replace = FALSE, prob = NULL, groups = NULL, orig.ids = FALSE, drop.unused.levels = FALSE, ...)

Arguments

x
Either a vector of one or more elements from which to choose, or a positive integer.
size
a non-negative integer giving the number of items to choose.
replace
Should sampling be with replacement?
prob
A vector of probability weights for obtaining the elements of the vector being sampled.
groups
groups to sample within (works much like groups in lattice plots)
orig.ids
a logical; should origianal ids be included in returned data frame?
...
additional arguments passed to sample
fixed
a vector of column names
shuffled
a vector of column names
invisibly.return
a logical, should return be invisible?
drop.unused.levels
a logical, should unused levels be dropped?

Details

These functions are wrappers around sample providing different defaults and natural names.

Examples

Run this code
# 100 Bernoulli trials -- no need for replace=TRUE
resample(c(0,1), 100)
deal(Cards, 13)    # A Bridge hand
shuffle(Cards)

Run the code above in your browser using DataLab