[R-SIG-Finance] Use apply/lapply/tapply functions

Uri Shimron UriShimron at optiver.com
Thu Sep 4 12:06:20 CEST 2008


I think that the R Help Desk article in the latest R News
(http://cran.r-project.org/doc/Rnews/Rnews_2008-1.pdf) on 'How can I
avoid this loop or make it faster' is a very nice and succinct article
on this issue.

In your case you could try to use
#WARNING: the line below has not been tested at all!
test <-
mapply(myfunction,p1=1:100,p2=1:100,MoreArgs=list(dataset=x1,y1=y1,y2=y2
,y3=y3))
#you may have to play a bit with SIMPLIFY
#see ?mapply

Hope this helps!

Uri Shimron


-----Original Message-----
From: r-sig-finance-bounces at stat.math.ethz.ch
[mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Rob Steele
Sent: Wednesday 03 September 2008 23:22
To: r-sig-finance at stat.math.ethz.ch
Subject: Re: [R-SIG-Finance] Use apply/lapply/tapply functions

Jorge Nieves wrote:
> Ok...
> 
> So in short, there is really not much computational advantage in terms
of time savings when using apply versus for loop, right? Is it fair to
say advantage of apply over "for loops"  is the "verorizing' and
compactness of the code?
> 
> Thanks,
> 
> Jorge

Um, no.  Or rather, you're half right.  apply() and it's siblings are 
mere syntactic sugar to help you write clean code.  They are implicit 
loops that still have to loop over whatever it is you would loop over in

an explicit loop.  Vectorization is something else altogether and is a 
big win if you can use it.  It can eliminate loops altogether, or rather

push them down into a hyper-optimized numerical library.

Good luck.

_______________________________________________
R-SIG-Finance at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

***************************************************************************
This email and any files transmitted with it are confide...{{dropped:11}}



More information about the R-SIG-Finance mailing list