A Google search on "lapply vs for r" or "lapply vs loop r" might have saved you 
some trouble. Many people have debunked this myth. Strangely they all start out 
with "everyone knows" or "it is commonly said that." I'm sure someone must have 
said it, but no one seems to be able to provide an authoritative citation 
before proceeding to demonstrate that it is false.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jesús Para 
Fernández
Sent: Monday, August 7, 2017 9:30 AM
To: r-help@r-project.org
Subject: [R] Has For bucle be impooved in R

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.

Reply via email to