[R] vectorized approach to cumulative sampling

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Thu Apr 7 23:46:12 CEST 2005


On 07-Apr-05 Daniel E. Bunker wrote:
> Hi All,
> 
> I need to sample a vector ("old"), with replacement, up to the point 
> where my vector of samples ("new") sums to a predefined value 
> ("target"), shortening the last sample if necessary so that the total 
> sum ("newsum") of the samples matches the predefined value.
> 
> While I can easily do this with a "while" loop (see below for example 
> code), because the length of both "old" and "new" may be > 20,000, a 
> vectorized approach will save me lots of CPU time.
> 
> Any suggestions would be greatly appreciated.
> 
> Thanks, Dan

Hi Dan,
You should be able to adapt the following vectorised approach
to your specific needs:

  old<-0.001*(1:1000)
  new<-sample(old,10000,replace=TRUE,prob=p)
  target<-200
  min(which(cumsum(new)>target))

## [1] 385

This took only a fraction of a second on my medium-speed machine.
If you get an "Inf" as result, then 'new' doesn't add up to
'target', so you have to extend it.

Hoping this helps,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 07-Apr-05                                       Time: 22:46:12
------------------------------ XFMail ------------------------------




More information about the R-help mailing list