# Block Randomization with Random Block Sizes

The following code illustrates how to generate the randomization treatment assignments for a randomized clinical trial assuming block randomization with random block sizes.
• The example code highlighted below is taken from this site.
• Also check out the `http://cged.genes.nig.ac.jp/RGM2/pkg.php?p=blockrand blockrand` R add-on package.

```# Example: --> for two treatments
b <- 20     # number of blocks
s <- c(2,4) # number of subjects per block
set.seed(1) # so can reproduce results
m <- sample(s, b, replace=T)  # actual number of subjects over blocks
# Get treatment assignments for each block, randomized within blocks
treats <- vector('list', b)
for(i in 1:b) treats[[i]] <-
sample(c(rep('a',m[i]/2),rep('b',m[i]/2)))
treats  # print for statistician use
unlist(treats)  # print blinded to block boundaries

#---------------------------------------------------------------------

# Second example:
# --> generate lists for 6 sites
# --> N = 100 per site
# --> 3 treatment arms (A, B, C)
# --> blocks of size 3 and 6
# *** Generate a list that is at least 600 elements long ***
b <- 136     # number of blocks
s <- c(3,6) # number of subjects per block
set.seed(1) # so can reproduce results
m <- sample(s, b, replace=T)  # actual number of subjects over blocks
# Get treatment assignments for each block, randomized within blocks
#   --> i.e. randomized permuted block list
treats <- vector('list', b)
for(i in 1:b) treats[[i]] <-
sample(c(rep('A',m[i]/3), rep('B',m[i]/3), rep('C', m[i]/3)))
treats  # print for statistician use
unlist(treats)  # print blinded to block boundaries
# Check the length of treats --- Need 600 total elements
length(unlist(treats))
# Check the randomization using table:
table(unlist(treats))

# NOTE: the above lists is 603 elements long, but we only need the
#   first 600 elements
# NOTE: the treatments are no longer equally distributed
#   among the 600 patients
table(unlist(treats)[1:600])
# Create list as dataframe
x<-data.frame(id=1:600, treatment=unlist(treats)[1:600])
write.table(x, "randlists.csv", sep=",", row.names=FALSE, quote=FALSE)
```
Topic revision: r4 - 07 Mar 2008, FrankHarrell

• Biostatistics Webs Copyright © 2013-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback