
Easy randomization in R
Rรฉmi Thรฉriault
October 14, 2019
Source:vignettes/randomize.Rmd
randomize.RmdGetting started
Hereโs how to easily nice_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.
Load the rempsyc package:
Note: If you havenโt installed this package yet, you will need to install it via the following command:
install.packages("rempsyc"). Furthermore, you may be asked to install the following packages if you havenโt installed them already (you may decide to install them all now to avoid interrupting your workflow if you wish to follow this tutorial from beginning to end):
install_if_not_installed("flextable")Between-Group Design
Basic output with default options (3 conditions, 9 participants, between-group design):
set.seed(100)
nice_randomize()## id Condition
## 1 1 b
## 2 2 a
## 3 3 c
## 4 4 c
## 5 5 a
## 6 6 b
## 7 7 b
## 8 8 c
## 9 9 a
Note: You will notice that I have set a seed with the
set.seed()function. For reproducibility reasons, users are also encouraged to set a seed before using this function.
Specify design, number of conditions, number of participants, and names of conditions:
set.seed(100)
nice_randomize(
design = "between",
Ncondition = 4,
n = 8,
condition.names = c("BP", "CX", "PZ", "ZL")
)## id Condition
## 1 1 CX
## 2 2 PZ
## 3 3 ZL
## 4 4 BP
## 5 5 PZ
## 6 6 BP
## 7 7 CX
## 8 8 ZL
# 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
set.seed(100)
nice_randomize(design = "within")## id Condition
## 1 1 b - a - c
## 2 2 c - a - b
## 3 3 b - c - a
## 4 4 b - c - a
## 5 5 c - b - a
## 6 6 c - a - b
## 7 7 c - b - a
## 8 8 a - b - c
## 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.).
set.seed(100)
nice_randomize(
design = "within",
Ncondition = 4,
n = 6,
condition.names = c("SV", "AV", "ST", "AT")
)## id Condition
## 1 1 AV - ST - AT - SV
## 2 2 ST - SV - AV - AT
## 3 3 AT - ST - AV - SV
## 4 4 AV - ST - AT - SV
## 5 5 ST - AT - SV - AV
## 6 6 AT - AV - SV - 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:
set.seed(100)
nice_randomized_subjects <- nice_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"
)
)
head(nice_randomized_subjects)## id Condition Date/Time SONA ID Age/Gd. Handedness Tester Notes
## 1 1 AV - ST - AT - SV NA NA NA NA NA NA
## 2 2 ST - SV - AV - AT NA NA NA NA NA NA
## 3 3 AT - ST - AV - SV NA NA NA NA NA NA
## 4 4 AV - ST - AT - SV NA NA NA NA NA NA
## 5 5 ST - AT - SV - AV NA NA NA NA NA NA
## 6 6 AT - AV - SV - ST NA NA NA NA NA NA
Save data frame to Word
- Save the resulting dataframe as a
nice_table, and then to Word:
runsheet <- nice_table(nice_randomized_subjects)
flextable::save_as_docx(runsheet, path = "runsheet.docx")
# 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 .docx extension of the file.After saving your dataframe to Word, open it in Word.
Make your document horizontal by going to the Layout tab, Page Setup section, then clicking on the โOrientationโ button and selecting the Landscape option.

- Select whole table.

- Left align table.

- Turn off preferred width in table properties.


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

- Next adjust the columns width to fit your page (especially the โNotesโ column, which should be wider).

- If necessary, pull the table left with the little square pound Move Table Column button in top margin to create more space (note that the table should be selected first).

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

- 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โ).

- 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).

- 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!
Thanks for checking in
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. Feel free to contact me for comments, questions, or requests to improve this function at https://github.com/rempsyc/rempsyc/issues. See all tutorials here: https://remi-theriault.com/tutorials.