slice_sample()1. Randomly choose 3 teachers per grade level
Review the data (d1)
# A tibble: 14 x 2
id grade
<dbl> <dbl>
1 10 3
2 11 3
3 12 3
4 13 4
5 14 4
6 15 4
7 16 3
8 17 3
9 18 4
10 19 4
11 20 3
12 21 4
13 22 4
14 23 3
I want to send surveys out to 3 teachers per grade level so I want to randomly choose those teachers from our existing roster.
Let’s first see how many teachers we have per grade level.
d1 %>%
janitor::tabyl(grade)
grade n percent
3 7 0.5
4 7 0.5
Next we can create our random samples.
First we need to use base::set.seed() and choose any
number to add as my seed. Setting this seed ensures that if I ever run
this code again at a later time, I will get the same random sample each
time. Very important!
Next we can use dplyr::group_by() to group our data by
grade level and then use dplyr::slice_sample() to randomly
select our cases. We use the argument n to say how many cases
we want from each group.
base::set.seed(1234)
d1 %>%
dplyr::group_by(grade) %>%
dplyr::slice_sample(n = 3)
# A tibble: 6 x 2
# Groups: grade [2]
id grade
<dbl> <dbl>
1 16 3
2 11 3
3 17 3
4 18 4
5 13 4
6 19 4
Return to Randomize