Stratified Sampling Using Fraction of Rows
In the above example, we can get stratified data using fraction of rows by using function sample_frac().
#Step 1: Package Installation
install.packages("dplyr")
library(dplyr)
#Step 2: Creating data frame
df <- data.frame(group= rep(c('Teachers', 'Students', 'Workforce', 'Guests'), each=150),
gpa = rnorm(600, mean=90, sd=3))
#Step 3: Obtain stratified sample
strat_sample <- df %>%
group_by(group) %>%
sample_frac(size=.20)
#Step 4: find frequency of people from each group
table(strat_sample$group)
Output:
Teachers Students Workforce Guests
30 30 30 30
Stratified Sampling in R
In this article, we will discuss what is Stratified Sampling and how we can perform Stratified Sampling in the R Programming Language.