[R] using lapply

William Dunlap wdunlap at tibco.com
Thu Mar 10 18:26:01 CET 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of 
> rex.dwyer at syngenta.com
> Sent: Thursday, March 10, 2011 8:47 AM
> To: ligges at statistik.tu-dortmund.de; arun.kumar.saha at gmail.com
> Cc: r-help at r-project.org
> Subject: Re: [R] using lapply
> 
> But no one answered Kushan's question about performance 
> implications of for-loop vs lapply.
> With apologies to George Orwell:
> "for-loops BAAAAAAD, no loops GOOOOOOD."

While using no loops is faster, lapply has
a loop in it and isn't much different in
speed from the equvialent for loop.  The big
advantage of the *apply functions is that
they can make your code easier to understand.
Here are some times for various ways of computing
log(1:1000000).  This example is probably close
to a worst-case scenario for the for loop, since
the time is dominated by the [<- operation.
Using the various *apply functions can get you a
speed-up of c. 4x, which is nice, but the vectorized
log gives a speed-up of c. 15x over the fastest of
the loops.  I think the for-loop method is ungainly
because it obscures to flow of the data, but there is
no accounting for taste.

  > system.time({ val.for <- numeric(1e6);for(i in
seq_len(1e6))val.for[i]<-log(i)})
     user  system elapsed 
     7.03    0.02    7.19 
  > system.time({ val.sapply <- sapply(seq_len(1e6), log) })
     user  system elapsed 
     6.59    0.03    6.80 
  > system.time({ val.lapply <- unlist(lapply(seq_len(1e6), log)) })
     user  system elapsed 
     2.48    0.00    2.52 
  > system.time({ val.vapply <- vapply(seq_len(1e6), log, FUN.VALUE=0)
})
     user  system elapsed 
     1.74    0.00    1.76 
  > system.time({ val.log <- log(seq_len(1e6)) })
     user  system elapsed 
     0.12    0.00    0.12 
  > identical(val.vapply,val.sapply) && identical(val.vapply,val.for) &&
identical(val.vapply,val.lapply) && identical(val.vapply,val.log)
  [1] TRUE

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> 
> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Uwe Ligges
> Sent: Thursday, March 10, 2011 4:38 AM
> To: Arun Kumar Saha
> Cc: r-help at r-project.org
> Subject: Re: [R] using lapply
> 
> 
> 
> On 10.03.2011 08:30, Arun Kumar Saha wrote:
> > On reply to the post
> > http://r.789695.n4.nabble.com/using-lapply-td3345268.html
> 
> Hmmm, can you please reply to the original post and quote it?
> You mail was not recognized to be in the same thread as the message of
> the original poster (and hence I wasted time to answer it again).
> 
> Thanks,
> Uwe Ligges
> 
> 
> 
> 
> > Dear Kushan, this may be a good start:
> >
> > ## assuming 'instr.list' is  your list object and you are applying
> > my.strat() function on each element of that list, you can use lapply
> > function as
> > lapply(instr.list, function(x) return(my.strat(x)))
> >
> > Here resulting element will again be another list with 
> length is same as the
> > length of your original list 'instr.list.'
> >
> > Instead if the returned object for my.strat() function is a 
> single number
> > then you might want to create a vector instead list, in 
> that case just use
> > 'sapply'
> >
> > HTH
> >
> > Arun,
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
> 
> ______________________________________________
> 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.
> 
> 
> 
> 
> message may contain confidential information. If you are not 
> the designated recipient, please notify the sender 
> immediately, and delete the original and any copies. Any use 
> of the message by you is prohibited. 
> ______________________________________________
> 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