RE: [R] vectorized approach to cumulative sampling

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
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:

  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@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 07-Apr-05                                       Time: 22:46:12
------------------------------ XFMail ------------------------------

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html 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