I would like to sample values, but have a constraint in place that demands two values are at least window apart. This would be akin to sampling days in a year, but setting the window to be at least a fortnight apart. So far I’ve tried it like this
check.diff <- TRUE
window <- 14
while (check.diff == TRUE) {
sampled.session <- sort(sample(1:365, size = 5, replace = FALSE))
check.diff <- any(diff(sampled.session) < window)
}
This works nicely if the window constraint is small. If one specifies a rather large value, this can become an infinite loop. While I can insert all sorts of checks and maximum number of iterations, I was wondering if there’s a smarter way of attacking this?
One way to do this is by removing candidates from the population from which you take the sample:
Another way would be