user3327637
user3327637

Reputation: 109

Picking random elements from a vector but excluding certain numbers each time

I have this vector

 K=c(1,2,3,4,5,6,8,10,12,14)

I want to pick 2 random elements from K such that my output never includes 6 or 14 or both each time. How can i do this for it to have output like if i used

 S=c(1,2,3,4,5,8,10,12)
 sample(S,2)

Upvotes: 2

Views: 109

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

You may take 6 and 14 out of the vector of candidates to sample from, as in

sample(setdiff(K, c(6, 14)), 2) 

Upvotes: 3

Related Questions