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

Enrico Schumann enricoschumann at yahoo.de
Wed Sep 3 17:27:46 CEST 2008

i suppose that this is rather an r-help question; however, if you can use a
function from the ``apply-family'', it should usually be far faster than a

N <- 100000
x <- array(0,dim=c(N,1))

# loop
pcm <- proc.time()
for (i in 1:N){
x[i] <- rnorm(1)
p1 <- proc.time()-pcm

# apply
y <- array(0,dim=c(N,1))
pcm <- proc.time()
y <- apply(y,2,rnorm)
p2 <- proc.time()-pcm

# compare time needed
# compare results

but, if you can use apply, then you probably did not really need a loop in
the first place, as your procedure is not really sequential (in the sense
that the computation in i+1 really required the computation from step i)

-----Ursprüngliche Nachricht-----
Von: r-sig-finance-bounces at stat.math.ethz.ch
[mailto:r-sig-finance-bounces at stat.math.ethz.ch] Im Auftrag von Rob Steele
Gesendet: Mittwoch, 3. September 2008 16:52
An: r-sig-finance at stat.math.ethz.ch
Betreff: Re: [R-SIG-Finance] Use apply/lapply/tapply functions

The looping functions (apply/lapply/tapply) can make your code cleaner and
easier to read but they can't speed it up.  For that you need to make the
stuff in the loop faster, perhaps by vectorizing parts you're currently
doing serially.

Jorge Nieves wrote:
> Hi,
> I have  a function that takes in a dataset ( a matrix of m rows by n 
> columns), and five additional "constant" parameters, p1,p2,y1,y2,y3. 
> The function perform a series of operations and transformations on the 
> dataset, and returns a table of results.
> I have tested the function repeatedly and it works fine.
> However, I would like to generate a grid of results from myfunction 
> for different values of two of the input parameters: p1, and p2.
> I have tried using for loops, and they work, but the computation time 
> is a too long.  I would like to use the apply/lapply/tapply functions 
> to avoid using for loops, what ever works !!!
> Can someone recommend how to use these function to parameterize only a 
> subset of the inputs into the function, i.e p1, and p2?
> Any tips/recommendations will be appreciated.
> myfunction = function (dataset, p1,p2,y1,y2,y3) {
> Line1
> Line2
> Line3
> ::::::::
> :::::::
> :::::::
> Return(res.table)
> }

R-SIG-Finance at stat.math.ethz.ch mailing list
-- Subscriber-posting only.
-- If you want to post, subscribe first.
No virus found in this incoming message.
Checked by AVG - http://www.avg.com


More information about the R-SIG-Finance mailing list