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):
Basic output with default options (3 conditions, 9 participants, between-group design):
## 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.
Basic output with default options (3 conditions and 9 participants) but with within-subject design specified
## 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
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
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.
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!
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.