我想为我的起始位置向量中的每个元素随机添加一个 1 到 15 之间的随机数(每个元素的随机抽取),并且我从不希望任何元素彼此之间的距离在 4 以内(例如,如果一个元素是= 到 20 那么我不希望下一个元素低于 25)。我写的有没有做到这一点,还有更好的方法吗?
startingpositions <- c(seq(5, 110-15, 15),seq(115, 220-15, 15),seq(225, 330-15, 15),seq(335, 440-15, 15))
positions <- c()
x <- 0
for (j in startingpositions)
# for each element of my vector
{
sub.samples <- setdiff(1:15 + j, seq(x-4,x+4,1))
# create the list of numbers it's ok to draw from (based on x which is = to my previous element). Only draw from numbers 4> than x
x <- sample(sub.samples, 1)
# create new x for my current element from sub samples
positions <- c(positions,x)
#add x to my positions vector
}