在你的代碼中,你在開始時創建一次隨機向量,然後你複製那個(固定)向量100次。你需要做的是在每次迭代中重複隨機抽樣。你可以例如做到這一點使用:
## Function to create your random vector:
createVector <- function(ii){
r1 <- c(1, 3, 2, 1, 2,
sample(1:4, 1, replace = TRUE), 4,
sample(1:4, 1, replace = TRUE), 3,
sample(1:4, 1, replace = TRUE),
sample(1:4, 1, replace = TRUE), 2,
sample(1:4, 1, replace = TRUE), 1,
sample(1:4, 1, replace = TRUE), 3)
}
## Replicate 100 times:
set.seed(1234)
rp <- replicate(100, createVector())
head(rp)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
# [1,] 1 3 2 1 2 1 4 3 3 3 3 2 4 1
# [2,] 1 3 2 1 2 1 4 1 3 3 3 2 3 1
# [3,] 1 3 2 1 2 2 4 4 3 2 4 2 2 1
# [4,] 1 3 2 1 2 1 4 1 3 2 2 2 1 1
# [5,] 1 3 2 1 2 1 4 4 3 3 4 2 4 1
# [6,] 1 3 2 1 2 2 4 2 3 2 3 2 1 1
# [,15] [,16]
# [1,] 3 3
# [2,] 3 3
# [3,] 2 3
# [4,] 1 3
# [5,] 1 3
# [6,] 4 3
謝謝您的回答,併爲延誤感到抱歉,我真的很忙。問候, –