RE: [R] vectorized approach to cumulative sampling

From: Ted Harding <>
Date: Fri 08 Apr 2005 - 07:46:12 EST

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:

  new<-sample(old,10000,replace=TRUE,prob=p)   target<-200

## [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,

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

______________________________________________ mailing list PLEASE do read the posting guide! Received on Fri Apr 08 08:03:21 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:04 EST