Basic statistics in R 309
the values are not replace d. By default, size is equal to the size of x. Since
replace is also FALSE by default, simply calling sample(x) will generate a
random permutation of x. Finally, the parameter prob can be used to specify
a vector with the probabilities of picking individual elements from x.
Let us use this function to construct a sample representing 15 random
throws of an unbiased dice:
> x=1:6
> y=sample(x,15,replace=T)
> y
[1] 2 3 4 3 4 3 6 1 4 1 5 6 1 1 5
or 10 tosses of a coin that was biased such that the head is 10% more likely
than the tail:
> sample(c("h","t"),10,replace=T,c(0.6,0.4))
[1] "t" "t" "h" "h" "t" "t" "t" "h" "h" "h"
In practical data analysis, the function sample is often used in Monte-Carlo
type simulations ...