20.1 Simple Sampling
Simple (random) Sampling
library(dplyr)
iris_df <- iris
set.seed(1)
sample_n(iris_df, 10)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.8 2.7 4.1 1.0 versicolor
#> 2 6.4 2.8 5.6 2.1 virginica
#> 3 4.4 3.2 1.3 0.2 setosa
#> 4 4.3 3.0 1.1 0.1 setosa
#> 5 7.0 3.2 4.7 1.4 versicolor
#> 6 5.4 3.0 4.5 1.5 versicolor
#> 7 5.4 3.4 1.7 0.2 setosa
#> 8 7.6 3.0 6.6 2.1 virginica
#> 9 6.1 2.8 4.7 1.2 versicolor
#> 10 4.6 3.4 1.4 0.3 setosa
library(sampling)
# set unique id number for each row
iris_df$id = 1:nrow(iris_df)
# Simple random sampling with replacement
srswor(10, length(iris_df$id))
#> [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1
#> [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
#> [75] 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0
#> [112] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [149] 0 0
# Simple random sampling without replacement (sequential method)
srswor1(10, length(iris_df$id))
#> [1] 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [75] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [112] 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
#> [149] 0 0
# Simple random sampling with replacement
srswr(10, length(iris_df$id))
#> [1] 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 0 0 0
#> [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
#> [112] 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
#> [149] 0 0
library(survey)
data("api")
srs_design <- svydesign(data = apistrat,
weights = ~pw,
fpc = ~fpc,
id = ~1)
Identify missing points between sample and collected data