Nicole Origami Fox
Nicole Origami Fox

Reputation: 5

How do I correctly randomize this specific data?

I would like to randomize Tests and the order of their versions. My data should look like this in the end:

> #  subject    Test     t1 t2 t3 t4
> # 1      1    PFT      A  B  C  D
> # 2      1    AWT      B  C  D  A
> # 3      1    BWFT     (...)
> # 4      1    AUT
> # 5      1    DPD-WN
> # 6      1    DPT-NI
> # 7      2    AWT
> # (...)  250

I have found something that helps me partly:

seed <- 42 
blocksize <- 6
N <- 250 x 6

set.seed(seed)

subject = rep(1:ceiling(N/blocksize), each = blocksize)

a1 = data.frame(subject, rand=runif(length(subject)), envelope= 1:length(subject))

a2 = a1[order(a1$subject,a1$rand),]

a2$Test = rep(c("BWFT", "DPT-NI", "DPD-WN", "AUT", "PFT", "AWT"),times = length(subject)/6)

assign = a2[order(a2$envelope),]

head(assign,12)

Can you please help me with the rest? Thank you kindly!

I'm getting closer:

(versions <- replicate(10, sample(c("A","B","C","D"), 4, replace=F)))

Upvotes: 0

Views: 55

Answers (1)

MrFlick
MrFlick

Reputation: 206197

You can generate a matrix of the values you need with

tt <- t(replicate(nrow(a2), sample(LETTERS[1:4])))
colnames(tt) <- paste0("t", 1:4)

and then just combine that into your data frame

a2 <- cbind(a2, tt)
head(a2)
#   subject      rand envelope   Test t1 t2 t3 t4
# 3       1 0.2861395        3   BWFT  A  B  D  C
# 6       1 0.5190959        6 DPT-NI  C  B  A  D
# 5       1 0.6417455        5 DPD-WN  D  B  A  C
# 4       1 0.8304476        4    AUT  D  C  A  B
# 1       1 0.9148060        1    PFT  C  A  B  D
# 2       1 0.9370754        2    AWT  B  D  A  C

Upvotes: 1

Related Questions