Experiment 6
Implementing Random Sampling in R
Simple Random Sampling
# simple random sampling
sample(1:10,10)
# w/o replace
sample(1:10,replace = FALSE)
# sample strings
sample(c("red","green"),10,replace = T, prob = c(0.6,0.4))
Systematic Sampling
# systematic sampling
install.packages("TeachingSampling" )
library (TeachingSampling)
P <- c("Mon-8", "Tues-4", "Wed-4", "Thurs-6", "Fri-7", "Sat-45", "Sun-34", "Mon-21", "Tues-11", "Wed-34", "Thurs-16", "Fri-10", "Sat-17", "Sun-
19" )
#systematic sample from a population of 14 with every 2nd included from the populaion P
systematic_sample <- S.SY (14, 2)
systematic_sample
P[systematic_sample]
Stratified Sampling
library(dplyr)
set.seed(1)
iris %>%
group_by(Species) %>%
sample_n(.,3)