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

问题描述 投票:0回答:1

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

> #  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)))
r random blocking
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.