Reputation: 261
Is it possible to sample such that it is Okay to have repeated sampling values (i.e. A,B,A ), but it cannot be in sequence(A,A,B), or return just one single value (A,A,A) or (B,B,B). The code below should always give me at least two values, but I don't want the returned values to be in sequence.
x<- 3
alphabet <- LETTERS[seq(1:26)]
a <- sample(alphabet, 2, replace = FALSE)
b <- sample(a, x, replace = TRUE, prob=c(0.5,0.5)) #can't replace
print(b)
Upvotes: 0
Views: 56
Reputation: 261
This code worked for my needs. Here I will try to explain.
# sample 2 letters without replacement.
# The sample letter will always be different.
a <- sample(LETTERS, 2, replace = FALSE)
# sample 3 times randomly.
b <- sample(a, 3, replace=TRUE)
# Reject sampling and repeat again if all the sampled values are the same.
# b %in% b[duplicated(b)]) return duplicated as TRUE, non duplicated as FALSE
# sort(unique(b %in% b[duplicated(b)])) returns only TRUE if the sample result consists of 1 sampled value. Keep doing that unless returns FALSE in the first position.
while(sort(unique(b %in% b[duplicated(b)]))[1] == TRUE){
b <- sample(a, x, replace=TRUE);
}
b
Upvotes: 0
Reputation: 9687
You could easily use rejection sampling, just by checking if your draw is acceptable and redrawing if not. You can use rle
to check lengths of sequences:
a <- sample(letters, 3, replace=TRUE)
while(any(rle(a)$lengths > 1)) a <- sample(letters, 3, replace=TRUE);
For size = 3
, you probably won't have to draw more than once or twice; for longer sequences, you might want something more sophisticated.
Upvotes: 2