Chapter 4 Example 1.4
Random sampling is easy in R. Here we will just address the simple case of assigning \(N\) units to \(g\) groups with sample sizes \(n_1 + n_2 + ... n_g = N\). The idea is to put the units in random order and then take the first \(n_1\) for group 1, the next \(n_2\) for group 2, and so on. Here we have ten units that we want to randomly assign to groups with sample sizes 4, 4, 2.
First we sample the numbers 1 through 10 to put them in random order.> set.seed(421) # to make repeatable
> r.order <- sample(10) # 1:10 in random order
> r.order
[1] 1 8 2 6 3 5 10 9 7 4
> subject.names <- c("Christine","Erica","Rebecca","Debbie","Laura","Karen",
+ "Sarah","Ellen","Alice","Amanda")
> subject.names[r.order]
[1] "Christine" "Ellen" "Erica" "Karen" "Rebecca" "Laura"
[7] "Amanda" "Alice" "Sarah" "Debbie"
> set.seed(421) # reset random number generator
> sample(subject.names,10) # sample 10 times from subject.names
[1] "Christine" "Ellen" "Erica" "Karen" "Rebecca" "Laura"
[7] "Amanda" "Alice" "Sarah" "Debbie"
The first four (Christine, Ellen, Erica, Karen) go in group 1, the second four (Rebecca, Laura, Amanda, Alice) in group 2, and the last two (Sarah, Debbie) in group 3. To me, this feels like assigning units to groups (groups are sorted, but units are in random order).
A second way that achieves an equivalent randomization is to put the group numbers (or treatment names) in a vector, and then randomly order the group numbers vector.> sample(c(1,1,1,1,2,2,2,2,3,3),10)
[1] 1 1 1 2 2 3 2 1 3 2
In this randomization, units 1, 2, 3, and 8 (Christine, Erica, Rebecca, Ellen) are in group 1, units 4, 5, 7, and 10 (Debbie, Laura, Amanda) are in group 2, and units 6 and 9 (Karen, Alice) are in group 3. To me, this feels more like assigning groups to units (units are sorted, but groups are in random order). However, the two approaches achieve equivalent randomizations.