[R] make sample() faster

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jun 14 12:04:36 CEST 2007


I think the problem is not sample (which is written in C), but that you 
are calling it with size=1.  Taking one sample with probabilities from a 
large discrete distribution is necessarily slow, but you can take a large 
sample for little more cost.

On Thu, 14 Jun 2007, Rainer M. Krug wrote:

> Hi
>
> I have a simulation which is relatively slow. I used Rprofile() and
> identified the calls to sample() as the culprit is sample():
>
> > summaryRprof("Documents/PostDoc/Aloe_Pillansii/R/create.out")
> $by.self
>                   self.time self.pct total.time total.pct
> "sample"               1.30     44.2       1.52      51.7
> "ifelse"               0.46     15.6       2.44      83.0
> .
> .
> .
>
> I am using sample() as follow:
> result <- sample(
>                  x=d.growth.seedling$growth,
>                  size=1,
>                  prob=d.growth.seedling$p,
>                  replace
>                  )
>
> d.growth.seedling$p and d.growth.seedling$growth have a length of 1024
> and are calculated initially by using density().
>
> My question: is there any way to make this faster, i.e. replace sample()
> as I use it with another faster algorithm (if necessary implemented in C)?
>
> Thanks in advance,
>
> Rainer
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list