Skip to content

Instantly share code, notes, and snippets.

@davebraze
Last active February 2, 2018 19:59
Show Gist options
  • Save davebraze/eb0d8bf8f1c68301bc449ea58e0bf1b2 to your computer and use it in GitHub Desktop.
Save davebraze/eb0d8bf8f1c68301bc449ea58e0bf1b2 to your computer and use it in GitHub Desktop.
Block randomize a stimulus list with dplyr
library(dplyr)
## Toy data
df <- data_frame(block=rep(1:3, each=4), item=rep(1:4,3),
cond=rep(LETTERS[1:2], 6),
stim=paste0(rep(letters[1:2],6), rep(1:6,each=2)))
## Randomize blocks and trials within blocks
df.rand <- df %>%
group_by(block) %>%
mutate(item = sample(item)) %>%
ungroup %>%
mutate(block = rep(sample(1:3), each=4)) %>%
arrange(block,item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment