[R] efficient code. how to reduce running time?

John Fox jfox at mcmaster.ca
Mon Jan 22 02:11:19 CET 2007


Dear Haris,

Using lapply() et al. may produce cleaner code, but it won't necessarily
speed up a computation. For example:

> X <- data.frame(matrix(rnorm(1000*1000), 1000, 1000))
> y <- rnorm(1000)
> 
> mods <- as.list(1:1000)
> system.time(for (i in 1:1000) mods[[i]] <- lm(y ~ X[,i]))
[1] 40.53  0.05 40.61    NA    NA
> 
> system.time(mods <- lapply(as.list(X), function(x) lm(y ~ x)))
[1] 53.29  0.37 53.94    NA    NA

In cases such as this, I don't even find the code using *apply() easier to
read.

Regards,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
-------------------------------- 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> Charilaos Skiadas
> Sent: Sunday, January 21, 2007 7:01 PM
> To: miraceti
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] efficient code. how to reduce running time?
> 
> On Jan 21, 2007, at 5:55 PM, miraceti wrote:
> 
> > Thank you all for lookin at it.
> > I'll fix the code to preallocate the objects.
> > and I wonder if there is a way to call anova on all the 
> columns at the 
> > same time..
> > Right now I am calling (Y~V1, data) from V1 to V50 thru a loop.
> > I tried (Y~., data) but it gave me different values from 
> the results I 
> > get when I call them separately, So I can't help but call 
> them 25,000 
> > times...
> 
> Have you looked at lapply, sapply, apply and friends?
> 
> Haris
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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