[R] Generate groups with random size but given total sample size

Arne Schulz arne.schulz at student.uni-kassel.de
Tue Jul 13 15:09:43 CEST 2010


Dear list,
I am currently doing some simulation studies where I want to compare different scenarios.
In particular, two scenarios should be compared: 10.000 cases in 100 groups with 100 cases per group and 10.000 cases in 100 groups with random group size (ranging from 5 to 500).

The first part is no problem:
> id <- seq(1,10000)
> group <- sort(rep(seq(1,100),100))

But I don't get along with the second scenario. Using sample does give me 100 groups with random cases, but generates more than 10.000 cases:
> set.seed(13)
> sum(sample(5:500, 100))
[1] 24583

Another way could be generating one sample at a time and sum the cases. But this would end up in trail & error to fit the 10.000 cases. Maybe it would break rules of probability, too.

I'm convinced that there should be another (and even better) way to handle this problem in R... :-)


Best regards,
Arne Schulz



More information about the R-help mailing list