如何使用4种 colored颜色 随机采样368幅图像的 colored颜色 顺序
- 不应直接重复("red" "red" "blue" would not be ok, but "red" "blue" "red" would be)
- 每一次出现的数量是否应该相等(每次92次,因为368/4=92)?
基于this,我已经在没有直接重复的情况下进行了采样:
library("dplyr")
set.seed(340)
values <- c("blue", "red", "green", "yellow")
len <- 368 # number of samples
samp <- sample(values, 1) # initialise variable
cols <- sapply(2:len, function(i) samp[i] <<- sample(setdiff(values, samp[i-1]), 1, replace = TRUE))
table(cols) # colors appear 94, 92, 88, 93 times
我try 构建一个for循环进行采样,直到达到if(table(cols)[1:4] == 92)
的确切数字,但它不起作用,而且在做了大量研究之后,我仍然不知道如何继续.如果能有提示和帮助,我会非常感激的!