Amnesty
Amnesty

Reputation: 31

sampling - restricting the number of times each element gets used

I'm looking for a way to sample numbers 1:40, 3812 times (length = 3812), with replacement - but restrict it such that each number doesn't get used more than 100 times. Is there a way to build this type of restriction in the sampling command (sample())?

Upvotes: 3

Views: 45

Answers (1)

talat
talat

Reputation: 70256

Here's an option:

sample(rep(1:40, 100), 3812)

Essentially, you start by building a vector of all possible numbers to sample from (rep(1:40, 100)), i.e. each number occurs as often as the maximum number of times it should be sampled (100) and then sample from it without replacement.

Upvotes: 2

Related Questions