| sample | R Documentation |
sample takes a sample of the specified size from the elementsofx using either with or without replacement.
sample(x, size, replace = FALSE, prob = NULL)sample.int(n, size = n, replace = FALSE, prob = NULL, useHash = (!replace && is.null(prob) && size <= n/2 && n > 1e7))
x | either a vector of one or more elements from which to choose,or a positive integer. See ‘Details.’ |
n | a positive number, the number of items to choose from. See‘Details.’ |
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 elementsof the vector being sampled. |
useHash |
|
Ifx has length 1, is numeric (in the sense ofis.numeric) andx >= 1, samplingviasample takes place from1:x.Note that thisconvenience feature may lead to undesired behaviour whenx isof varying length in calls such assample(x). See the examples.
Otherwisex can be anyR object for whichlength andsubsetting by integers make sense: S3 or S4 methods for theseoperations will be dispatched as appropriate.
Forsample the default forsize is the number of itemsinferred from the first argument, so thatsample(x) generates arandom permutation of the elements ofx (or1:x).
It is allowed to ask forsize = 0 samples withn = 0 ora length-zerox, but otherwisen > 0 or positivelength(x) is required.
Non-integer positive numerical values ofn orx will betruncated to the next smallest integer, which has to be no larger than.Machine$integer.max.
The optionalprob argument can be used to give a vector ofweights for obtaining the elements of the vector being sampled. Theyneed not sum to one, but they should be non-negative and not all zero.Ifreplace is true, Walker's alias method (Ripley, 1987) isused when there are more than 200 reasonably probable values: thisgives results incompatible with those fromR < 2.2.0.
Ifreplace is false, these probabilities are appliedsequentially, that is the probability of choosing the next item isproportional to the weights amongst the remaining items. The numberof nonzero weights must be at leastsize in this case.
sample.int is a bare interface in which bothn andsize must be supplied as integers.
Argumentn can be larger than the largest integer oftypeinteger, up to the largest representable integer in typedouble. Only uniform sampling is supported. Tworandom numbers are used to ensure uniform sampling of large integers.
Forsample a vector of lengthsize with elementsdrawn from eitherx or from the integers1:x.
Forsample.int, an integer vector of lengthsize withelements from1:n, or a double vector ifn >= 2^31.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
Ripley, B. D. (1987)Stochastic Simulation. Wiley.
RNGkind(sample.kind = ..) about random number generation,notably the change ofsample() results withR version 3.6.0.
CRAN packagesampling for other methods of weighted samplingwithout replacement.
x <- 1:12# a random permutationsample(x)# bootstrap resampling -- only if length(x) > 1 !sample(x, replace = TRUE)# 100 Bernoulli trialssample(c(0,1), 100, replace = TRUE)## More careful bootstrapping -- Consider this when using sample()## programmatically (i.e., in your function or simulation)!# sample()'s surprise -- examplex <- 1:10 sample(x[x > 8]) # length 2 sample(x[x > 9]) # oops -- length 10! sample(x[x > 10]) # length 0## safer version:resample <- function(x, ...) x[sample.int(length(x), ...)]resample(x[x > 8]) # length 2resample(x[x > 9]) # length 1resample(x[x > 10]) # length 0## R 3.0.0 and latersample.int(1e10, 12, replace = TRUE)sample.int(1e10, 12) # not that there is much chance of duplicates
Add the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.