Zero-fixed (re)sampling

Share:

Description

This function builds on sample to provide sampling from a vector, but with all zero entries fixed. This way, zfsample(c(0,1,0,2)) may result in (0,1,0,2) or (0,2,0,1), but the positions that were initially zero will remain zeroed.

Usage

1

Arguments

x

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

replace

Should sampling be with replacement?

Value

a vector of the same length of 'x' with elements drawn from 'x'.

Details

The actual sampling is done by sample, so its help page should be checked for details on the parameter handling. The parameter 'size' is always passed as length(x), and 'prob' is not supported.

Examples

1
2
3
4
5
6
7
8
# Sampling without replacement
zfsample(c(0,1,2,0,3,4,0))
# Sampling with replacement
zfsample(c(0,1,2,0,3,4,0), replace=TRUE)
# With no zeroes, zfsample just calls sample
set.seed(42); s1<-sample(c(1,2,3,4,5,6))
set.seed(42); s2<-zfsample(c(1,2,3,4,5,6))
all.equal(s1, s2)