5. Sampling: Practical 5a
Simple Random Sampling
For simple random sampling the function sample()
is all you need. Take a look at the documentation to find out how to use this function.
?sample
As you see, there are several arguments for the sample()
function. As you are now performing simple random sampling, you only have to specify:
- x: the vector you want to take a sample from, or an integer representing the vector from 1 up to that integer (so an input x=c(1,2,3,4,5) gives the same result as x=5).
- size: the number of items you want to choose
sample(x=10, size=3)This results for example in:
[1] 1 10 3
The command
set.seed()
ensures reproducibility of your result (i.e. the sample will be identical if you run the sample()
command again with the same seed). Try to take #2# times a sample with the seed #12# and then #2# times with the seed #67#
set.seed(12)
sample(10, 3)
[1] 2 7 3
set.seed(12)
sample(10, 3)
[1] 2 7 3
set.seed(67)
sample(10, 3)
[1] 4 1 6
set.seed(67)
sample(10, 3)
[1] 4 1 6
We can also apply the sample()
function to take a simple random sample from a variable in the BCI dataset directly.
When taking #10# random samples of only one variable (in this case agb) you can use the following command:
set.seed(48)
agb_sample <- sample(BCI$ agb, 10)
agb_sample
If however, you want to sample #10# trees with all the variables belonging to those trees, there is a little more work to do, because the sample()
function does not accept a dataframe as input for x. What you can do is specify the number of rows (= number of observations) in the dataframe as argument x, and take a random sample of these row numbers. You can use this row nubers as index to retrieve the samples from the dataframe. Remember that indexing a dataframe works by first specifying the row number, then the column numbers. If you want all columns you can leave the column numbers empty, e.g. BCI[1, ]
returns the first row of bci with all columns.
set.seed(48)
row_sample <- sample(nrow(BCI), 10)
row_sample
BCI_sample <- BCI[row_sample, ]
BCI_sample