3 ms·
*apply functions are loops underneath -- they only look better and save you time possibly wasted on growing some dynamically sized output structure. The way of
by mbq 12y ago
*apply functions are loops underneath -- they only look better and save you time possibly wasted on growing some dynamically sized output structure. The way of solving slow loop in R is to find package which implements it in C/Fortran (or write your own in case there is none).
- platz 12y agoI believe there is also the Rcpp package which lets you write inline compiled c++
- craigching 12y ago> * apply functions are loops underneath Yes, but aren't they native loops underneath? I've seen it said both ways, that * apply is faster than R loops and that *apply isn't faster than R loops. Would be nice if someone could definitively answer the question and back it up with some stats! :) EDIT: Thanks chuckcode, sibling post to this, I stand corrected :)
- chuckcode 12y agoIt'a actually a little complicated but if you're interested in the details check out this stack overflow thread [1]. High level summary is that lapply() and functions built on top of it do some work in native C and so are generally faster but not all of the *apply() functions are faster. [1] http://stackoverflow.com/questions/2275896/is-rs-apply-family-more-than-syntactic-sugar http://stackoverflow.com/questions/2275896/is-rs-apply-famil...
- mbq 12y agoThe problem here is not the for-loop itself but the time used by the R runtime on executing the mapped function multiplied by the number of iterations (this is BTW the main source of advantage for dynamic and GCed but JITed languages like JS or Julia).