Learn R Programming

mosaic (version 0.2-3)

sample: Random Samples and Permutations

Description

These extensions to the base::sample make resampling and randomization tests easier.

Usage

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

## S3 method for class 'matrix': 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 'factor': sample(x, size, replace = FALSE, prob = NULL, groups = NULL, orig.ids = FALSE, drop.unused.levels=FALSE, ...)

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

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

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

rflip(n=1, prob=.5, quiet=FALSE, verbose=!quiet) nflip(n=1, prob=.5, ...)

## S3 method for class 'cointoss': print(x,...)

Arguments

x
an object (typically a data frame, matrix, or vector)
size
size of the sample
n
number of coins to toss
replace
a logical indicating whether sampling is to be done with replacement.
prob
a vector of probability weights for obtaining the elements of the object being sampled, or the probability of heads for rflip and nflip.
groups
If present, shuffling is done within groups given by this vector. In this case size, if present, is ignored.
orig.ids
If TRUE and x is a data frame or a matrix, then the original locations of the data is recorded in the object returned.
quiet
If TRUE, suppress output.
verbose
If FALSE, suppress output.
drop.unused.levels
a logical. Remove unused levels from factors?
fixed
vector of columns that are not permuted
shuffled
vector of columns that are permuted
invisibly.return
a logical indicating whether result should be returned invisibly.
...
Additional arguments.

Value

  • an object of the same type as x unless x is an integer, which case a vector is returned.

Examples

Run this code
denominations <- c(2:10,c("J","Q","K","A"))
suits <- c("C","D","H","S")
cards <- as.vector(outer(denominations, suits, paste, sep="") )
shuffle(cards)
set.seed(52)
sample(cards, 13)
set.seed(52)
deal(cards, 13)      # same as sample(cards, 13)
resample(cards, 20)  # same as sample(..., replace=TRUE)
sample(cards, groups=rep(1:4, each=13))
rflip()              # flip 1 coin
rflip(20)            # flip 20 coins
nflip(20)            # flip 20 coins and only show the number of heads
table(do(1000) * nflip(20))   # a table based on flipping 20 coins 1000 times
table(do(1000) * nflip(20,.25))   # a table based on flipping 20 biased coins 1000 times

Run the code above in your browser using DataLab