[R] make sample() faster

Rainer M. Krug Rainer at krugs.de
Thu Jun 14 10:07:52 CEST 2007


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

-- 
NEW EMAIL ADDRESS AND ADDRESS:

Rainer.Krug at uct.ac.za

RKrug at sun.ac.za WILL BE DISCONTINUED END OF MARCH

Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation
Biology (UCT)

Leslie Hill Institute for Plant Conservation
University of Cape Town
Rondebosch 7701
South Africa

Fax:		+27 - (0)86 516 2782
Fax:		+27 - (0)21 650 2440 (w)
Cell:		+27 - (0)83 9479 042

Skype:		RMkrug

email:	Rainer.Krug at uct.ac.za
       	Rainer at krugs.de



More information about the R-help mailing list