Thanks to Bill Dunlap & Bert Gunter for replies to my sampling problem query.
Bill said:
sample() was misdesigned so you have to give it a scalar if
you want it work in simulations like yours. Replace
sample(x, size)
with
x[sample(length(x), size)]
And Bert had a neat work-around:
In the Help file it explicitly says that if x has length 1, then sampling
takes place from 1:x.
So do the sampling the other way round: That is sample all your single
numbers first via singles<-sample(x,n,replace=F) where n is the number of
sets in the partition that contain a single element (you can put all these
together if there is more than one such set because you know they each just
contain the ith element in the sample of n); and then partition
setdiff(x,single)as you did before.
########################
Original Question:
I am doing an exhaustive sample, of random sizes from a population, say 1:10.
The sample sizes are controlled but the sum of the sample sizes ==
length(population) and the population is reduced by the latest sample at
each pass.
sample(1:10, size) works fine unless at the end, population is of length 1,
then,
per the correct operation of sample(), I get, not the last digit but an
integer between 1 and the last digit. So, I must continually test for that
condition.
Is there a smarter way to reshuffle this deck into discreet units?
thanks,
john
John Fennick
jhf2@adelphia.net
Tel: 603.526.4023
134 Brookside Drive
New London, NH 03257
USA
|