The lapply loop and the for loop have very similar speed characteristics. Differences seen are almost always due to how you use memory in the body of the loop. This fact is not new. You may be under the incorrect assumption that using lapply is somehow equivalent to "vectorization", which it is not. -- Sent from my phone. Please excuse my brevity.
On August 7, 2017 7:29:58 AM PDT, "Jesús Para Fernández" <j.para.fernan...@hotmail.com> wrote: >Hi! > >I am doing a lapply and for comparaison and I get that for is faster >than lapply. > > >What I have done: > > > >n<-100000 >set.seed(123) >x<-rnorm(n) >y<-x+rnorm(n) >rand.data<-data.frame(x,y) >k<-100 >samples<-split(sample(1:n),rep(1:k,length=n)) > >res<-list() >t<-Sys.time() >for(i in 1:100){ > modelo<-lm(y~x,rand.data[-samples[[i]]]) > prediccion<-predict(modelo,rand.data[samples[[i]],]) > res[[i]] <- (prediccion - rand.data$y[samples[[i]]]) > >} >print(Sys.time()-t) > >Which takes 8.042 seconds > >and using Lapply > >cv.fold.fun <- function(index){ > fit <- lm(y~x, data = rand.data[-samples[[index]],]) > pred <- predict(fit, newdata = rand.data[samples[[index]],]) > return((pred - rand.data$y[samples[[index]]])^2) > } > > >t<-Sys.time() > >nuevo<-lapply(seq(along = samples),cv.fold.fun) >print(Sys.time()-t) > > >Which takes 9.56 seconds. > >So... has been improved the FOR loop on R??? > >Thanks! > > > > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >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@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.