Sampling is a method to select a subset of data from a statistical population, which can use the characteristics of the population to estimate the whole population. The following recipe will demonstrate how to generate samples in R.
Perform the following steps to understand data sampling in R:
- To generate random samples of a given population, the user can simply use the
sample
function:
> sample(1:10)
- To specify the number of items returned, the user can set the assigned value to the
size
argument:
> sample(1:10, size = 5)
- Moreover, the sample can also generate Bernoulli trials by specifying
replace = TRUE
(default isFALSE
):
> sample(c(0,1), 10, replace = TRUE)
- If we want to do a coin flipping trail, where the outcome is
Head
orTail
, we can use:
> outcome <- c("Head","Tail") > sample(outcome, size=1)
- To generate result for
100
times, we can use...