resample: More Random Samples

Description Usage Arguments Details Examples

View source: R/resample.R

Description

These functions simplify and unify sampling in various ways.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
resample(..., replace = TRUE)

deal(...)

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

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

## Default S3 method:
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

...

additional arguments passed to base::sample() or mosaic::sample().

replace

Should sampling be with replacement?

x

Either a vector of one or more elements from which to choose, or a positive integer.

prob

A vector of probability weights for obtaining the elements of the vector being sampled.

groups

a vector (or variable in a data frame) specifying groups to sample within. This will be recycled if necessary.

orig.ids

a logical; should original ids be included in returned data frame?

size

a non-negative integer giving the number of items to choose.

fixed

a vector of column names. These variables are shuffled en masse, preserving associations among these columns.

shuffled

a vector of column names. these variables are reshuffled individually (within groups if groups is specified), breaking associations among these columns. examples.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 100 Bernoulli trials -- no need for replace=TRUE
resample(0:1, 100)
tally(resample(0:1, 100))
if (require(mosaicData)) {
Small <- sample(KidsFeet, 10)
resample(Small)
tally(~ sex, data=resample(Small))
tally(~ sex, data=resample(Small))
# fixed marginals for sex
tally(~ sex, data=Small)
tally(~ sex, data=resample(Small, groups=sex)) 
# shuffled can be used to reshuffle some variables within groups
# orig.id shows where the values were in original data frame.
Small <- mutate(Small, 
   id1 = paste(sex,1:10, sep=":"),  
   id2 = paste(sex,1:10, sep=":"))
resample(Small, groups=sex, shuffled=c("id1","id2"))
}
deal(Cards, 13)    # A Bridge hand
shuffle(Cards)

ProjectMOSAIC/mosaic documentation built on Aug. 19, 2018, 9:14 a.m.