[R] vector

Charles C. Berry cberry at tajo.ucsd.edu
Tue Dec 2 06:25:38 CET 2008

On Mon, 1 Dec 2008, Hamid Hamid wrote:

> Dear All,
> I am trying to build a program which will take repeated samples without
> replacement from a population of values. The interesting catch is that I
> would like the sample values to be removed from the population, after each
> sample is taken.

You mean ONLY 'after each sample is taken'? That is,  you sample with 
replacement, then remove the sample from the population, sample again with 
replacement and then remove those, ...

If that is not what you mean, ordinary sampling WOR will do.

If the scheme I describe above is what you meant, read on.

Sometimes it is efficient to vectorize in a way that yields an incomplete 
answer, then patch the result, then patch the patched result, ...

Here is an example that implements drawing samples with replacement, but 
removing each sample before drawing the next (so a single sample could 
contain duplicates, but no pair of samples could contain the same 

> n <- c(3,3,4) # draw samples of size n[i]
> samp <- rep(seq(n),n) # index the sample
> trials <- sample(pop,sum(n),repl=TRUE) # fast but not always right
> index.first.like.me <- match(trials,trials)
> gotta.fix <- which( samp[ index.first.like.me ] < samp )
> trials[ gotta.fix ] <- sample( setdiff(pop,trials), length( gotta.fix ), repl=TRUE )
> index.first.like.me <- match(trials,trials)
> gotta.fix <- which( samp[ index.first.like.me ] < samp )
> length(gotta.fix) # see if there are any cases to fix
[1] 0

Obviously, you would build the part after the 'trials <-' line  into a 
while(){} loop , and you can easily improve upon 'setdiff( pop, trials )'.



> For example:
> pop<-c(1,5,14,7,9,12,18,19,65,54)
> sample(pop, 2) = lets say, (5,54)
> ## This is where I would like values (5, 54) to be removed from the
> population vector, giving a new "current" population vector:
> "new" pop = [1,14,7,9,12,18,19,65]
> and has length 8 instead of 10.
> In the cases when the size of pop and deriven sample of it is enough large
> using the following command is not helpful.
> newpop<-pop[-c(2,10)]
> One could simplify my question in this way: how we can exclude a sub vector
> values from a super  vector value (i.e sub vecor values are subset of super
> vector values).
> Thanks in advance.
> Hamid
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

More information about the R-help mailing list