# Easy randomization in R

## Getting 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:

library(rempsyc)

Note: If you haven’t installed this package yet, you will need to install it via the following command: install.packages("rempsyc").

## Between-Group Design

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

nice_randomize()
##   id Condition
## 1  1         b
## 2  2         a
## 3  3         c
## 4  4         c
## 5  5         a
## 6  6         b
## 7  7         c
## 8  8         a
## 9  9         b

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

nice_randomize(design = "between",
Ncondition = 4,
n = 8,
condition.names = c("BP","CX","PZ","ZL"))
##   id Condition
## 1  1        PZ
## 2  2        BP
## 3  3        ZL
## 4  4        CX
## 5  5        CX
## 6  6        ZL
## 7  7        PZ
## 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

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

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

nice_randomize(design = "within",
Ncondition = 4,
n = 6,
condition.names = c("SV","AV","ST","AT"))
##   id         Condition
## 1  1 ST - SV - AT - AV
## 2  2 ST - AT - SV - AV
## 3  3 ST - AV - AT - SV
## 4  4 AT - SV - ST - AV
## 5  5 SV - AT - AV - ST
## 6  6 ST - AT - SV - AV
# 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:

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 - AT - ST - SV        NA      NA      NA         NA     NA    NA
## 2  2 SV - AV - AT - ST        NA      NA      NA         NA     NA    NA
## 3  3 AT - ST - SV - AV        NA      NA      NA         NA     NA    NA
## 4  4 AV - AT - SV - ST        NA      NA      NA         NA     NA    NA
## 5  5 SV - ST - AT - AV        NA      NA      NA         NA     NA    NA
## 6  6 SV - AV - AT - ST        NA      NA      NA         NA     NA    NA

### Save data frame to Word

1. 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.
1. After saving your dataframe to Word, open it in Word.

2. 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. Select whole table.

1. Left align table.

1. Turn off preferred width in table properties.

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

1. Next adjust 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 (note that the table should be selected first).

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

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!

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