如何正确随机化此特定数据?

时间:2018-11-09 17:06:49

标签: r random blocking

我想随机化测试及其版本的顺序。 我的数据最后应该像这样:

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

我发现了一些对我有帮助的东西:

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)

您能帮我忙吗? 谢谢!

我越来越近:

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

1 个答案:

答案 0 :(得分:1)

您可以使用

生成所需值的矩阵
tt <- t(replicate(nrow(a2), sample(LETTERS[1:4])))
colnames(tt) <- paste0("t", 1:4)

然后将其合并到您的数据框中

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