[R] apply question

Petr PIKAL petr.pikal at precheza.cz
Mon May 3 10:48:53 CEST 2010


Hi

r-help-bounces at r-project.org napsal dne 30.04.2010 23:11:54:

> Hello David,
> 
> On Apr 30, 2010, at 11:00 PM, David Winsemius wrote:
> > Note: Loops may be just as fast or faster than apply calls.
> > 
> How come!? is this true also for other similar functions: lapply, tapply 
and sapply?
> 
> Then the only advantage of these above is only syntactic sugar? 

Yes and no. If your loop(s) is poorly written it can be much slower then 
*apply operator but usually not from loop point of view but from the code 
itself. 

Regards
Petr

> 
> >> 
> >> indices <- replicate(B,sample(1:N, size=N, replace=TRUE))
> >> theta_star <- apply(indices,2,statistic,data)
> > 
> > Why not:
> > 
> > theta_star <- apply(indices,2, function(x) statistic(data, x) )
> > 
> > May end up as a data class that needs further work, depending on what 
> "statistic" return.
> > 
> Nice! thank you!
> 
> Best regards,
> Giovanni
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list