Here’s how to easily randomize for either within-subject or between-group!

Sure, you might just want to generate a random number n times, and hope each group gets approximately the same number of participants assigned to it. Or, you can make sure of this by making “blocks” without replacements. So if you had four groups, each block of four would contain each group once, in a random order, which you would then repeat. Let’s see this.

Let’s first source the function from my github (you need internet for that):


Between-Group Design

Basic output with default options (3 conditions, 9 participants, between-group design):

*Warning:* running the function below for the first time will install and load the following
package (if it is not already installed and loaded on your machine): plyr.
Note: This will run many lines of code on your console and could take a few minutes.
##   id Condition
## 1  1         c
## 2  2         a
## 3  3         b
## 4  4         a
## 5  5         c
## 6  6         b
## 7  7         b
## 8  8         c
## 9  9         a

Specify design, number of conditions, number of participants, and names of conditions:

randomize(design = "between", Ncondition = 4, n = 8, condition.names = c("BP","CX","PZ","ZL"))
##   id Condition
## 1  1        ZL
## 2  2        CX
## 3  3        BP
## 4  4        PZ
## 5  5        PZ
## 6  6        CX
## 7  7        ZL
## 8  8        BP
# Warning: sample size needs to be a multiple of your number of groups if using "between"!
# FYI: condition names stand for popular antidepressants:
# BP = Bupropion, CX = Celexa, PZ = Prozac, ZL = Zoloft.

Be aware that this function won’t work for between-group designs unless your sample size is a multiple of your number of groups (so for 4 groups, sample size can’t be 5, 6, or 7, but you can have 4, 8, 12, etc., participants). You can still try otherwise but you will get an error and a warning message. That is because it works by “blocks” so some operations won’t be able to be completed otherwise.

Within-Group Design

Basic output with default options (3 conditions and 9 participants) but with within-subject design specified:

randomize(design = "within")
##   id Condition
## 1  1 c - b - a
## 2  2 c - b - a
## 3  3 a - c - b
## 4  4 a - b - c
## 5  5 b - c - a
## 6  6 b - a - c
## 7  7 b - c - a
## 8  8 c - b - a
## 9  9 c - b - a

Specify design, number of conditions, number of participants, and names of conditions:

Note that you could call your conditions anything (like a, b, c, etc.).

randomize(design = "within", Ncondition = 4, n = 6, condition.names = c("SV","AV","ST","AT"))
##   id         Condition
## 1  1 ST - SV - AT - AV
## 2  2 AV - AT - ST - SV
## 3  3 SV - ST - AT - AV
## 4  4 ST - AV - SV - AT
## 5  5 AT - ST - SV - AV
## 6  6 SV - AT - AV - ST
# FYI: condition names stand for forms of multisensory stimulation:
# SV = Synchronous Visual, AV = Asynchronous Visual, ST = Synchronous Tactile, AT = Asynchronous Tactile.

Make a quick runsheet

A nice way to make a run sheet quickly (regardless of design) is by adding column names in advance with the col.names argument:

randomized_subjects <- randomize(design = "within", 
                                 Ncondition = 4, 
                                 n = 128,
                                 condition.names = c("SV","AV","ST","AT"), 
                                 col.names = c("id", "Condition", "Date/Time", "SONA ID", "Age/Gd.", "Handedness", "Tester", "Notes"))

##   id         Condition Date/Time SONA ID Age/Gd. Handedness Tester Notes
## 1  1 SV - ST - AV - AT      <NA>    <NA>    <NA>         NA     NA    NA
## 2  2 SV - AV - ST - AT      <NA>    <NA>    <NA>         NA     NA    NA
## 3  3 AT - AV - ST - SV      <NA>    <NA>    <NA>         NA     NA    NA
## 4  4 ST - SV - AT - AV      <NA>    <NA>    <NA>         NA     NA    NA
## 5  5 SV - AV - AT - ST      <NA>    <NA>    <NA>         NA     NA    NA
## 6  6 AV - AT - ST - SV      <NA>    <NA>    <NA>         NA     NA    NA

Save data frame as CSV file

These steps apply no matter what design you have used. Below, you have to specify where you want to save the table as .csv.

write.table(randomized_subjects,  # Save data frame to .csv file for later use
            file = "D:/R treasures/randomization.csv", # Specify own path
            sep = ',', # This indicates it's a CSV (comma separated values) file
            row.names = FALSE,
            col.names = TRUE,
            na = "") # replaces NA's with empty cells
# Change the path to where you would like to save it.
# If you copy-paste your path name, remember to use "R" slashes ('/' rather than '\').
# Also remember to specify the .csv extension of the file.

Export to Word/PDF

After saving your .csv file:

  1. Navigate to your newly created file, and open it in your dedicated spreadhseet program (e.g., Excel).
  2. Double click on column separator of “Condition” (column B) to make it large enough to see all conditions without them overlapping over the next column (otherwise it might create problems later on when copying over to Word).

  1. Select columns A to H (or whichever columns you have).

  1. Then copy selection (press CTR+C).
  2. Open your dedicated text-processing program (e.g., Word).
  3. Make your document horizontal by going to the Layout tab, Page Setup section, then clicking on the “Orientation” button and selecting the Landscape option.

  1. Paste selection (press CTR+V).
  2. Select whole table.

  1. Make required formatting arrangements: In the Home tab, Paragraph section, click on the Borders button, then click All Borders.

  1. Center all text by going to the table ‘Layout’ tab, Alignment section, then clicking on the Align Center button.

  1. Next ajust the columns width to fit your page (especially the “Notes” column, which should be wider)

  1. If necessary, pull the table left with the little square pound Move Table Column button in top margin to create more space.

  1. Increase the height of cells by selecting the table again then going to the table ‘Layout’ tab, Cell Size section, and pressing the Table Row Height arrow until there is about 8 participants per page (suggested height: 1.7 cm).

  1. Double-click on the header to edit it and add a title (e.g., “Study 3 Runsheet”); center and bold (suggested font size: 14).

  1. Under the title in smaller font (not bold; suggested size: 12), add the legend for the conditions (e.g., “SV = Synchronous Visual, AV = Asynchronous Visual, ST = Synchronous Tactile, AT = Asynchronous Tactile”).

16. Important(!): Make sure that your column names appear on every page by going to the table ‘Layout’ tab, Data section, and selecting Repeat Header Rows. Else you’ll always need to go back to the first page to remember what you should write where!

  1. Add a page number at the bottom of the page by going to the Insert tab, Header & Footer section, and click on the “Page Number” button, then selecting “Bottom of Page”, then Plain Number 3 (which is bottom right).

  1. Now ‘Save As’ using the F12 keyboard key, then select save as .pdf, and you’re done! :)

You can now use this runsheet yourself, print it, or send it to your research assistants!

Note: you might want to save that word document as you work on it to not lose it and perhaps reuse it in the future! \(~\)


Concluding Statement

Make sure to check out this page again if you use the code after a time or if you encounter errors, as I periodically update or improve the code.

You can always edit the function to suit your purposes, or contact me for questions or requests to modify this function at https://remi-theriault.com/contact! Thanks for reading my guide! :) \(~\)