Generate Random Samples or Permutations of Data

DESCRIPTION:
Produces a vector of length size of objects randomly chosen from the population.

USAGE:
sample(x, size=<<see below>>, replace=F, prob=<<see below>>)

REQUIRED ARGUMENTS:
x:
numeric, complex or character vector of data (the population) to be sampled, or a positive integer giving the size of the population, which is then taken to be seq(x). Missing values (NAs) are allowed.

OPTIONAL ARGUMENTS:
size:
sample size. The default is the same as the population size, and thus (with replace=FALSE) will generate a random permutation.
replace:
if TRUE, sampling will be done with replacement.
prob:
vector of probabilities the same length as x (or of length x, if x is a positive integer), giving probabilities of selection for each of the elements of x. If the elements of prob do not sum to one, they will be normalized so that they do. The default is equal probabilities for each element of the population.

VALUE:
if length(x)>1 a sample from x; otherwise, the result is a set of integers between 1 and x. If replace is FALSE and size is larger than the number of elements in the population, then the last elements of the result will be NA. If prob is specified it is used as the probabilities that the elements of x get selected.

DETAILS:
Missing values in x are treated like any other value. If x represents a population, it can have any atomic mode; in particular, it can be a vector of character strings.

If x has length 1 and thus is treated as the population size, it cannot be larger than the largest positive integer on the machine (.Machine$integer.max, 2147483647 on a 32 bit computer).


SEE ALSO:
runif to generate uniformly distributed real numbers.

EXAMPLES:
sample(state.name, 10)   # pick 10 unique states at random
sample(1e6, 75)  # pick 75 numbers between 1 and one million
sample(50)    # random permutation of numbers 1:50
sample(0:1,100,c(.3,.7),T)    # Bernoulli(.3) sample of size 100

# 20 uniformly distributed numbers on the integers 1:10 with replacement sample(10, 20, T)

sample(5, 24, prob=c(.3,.4,.1,.1,.1), replace=T)