PDA

View Full Version : for loops in R


cjmr
January 17th, 2010, 01:33 AM
Hello!

I just started R and am trying to convert C code to it. I need help in checking if the two codes below are the same.

for (i in 1:popsize) {
for (j in 1:maxvar) {
gBest<-sample(top,size=1)
velocity[i,j]<-.4* velocity[i,j] + 1 * runif(1) * (pbestsVar[i,j] - popVar[i,j]) + 1 * runif(1) * (archiveVar[gBest,j] - popVar[i,j])
}
}

and

i<-seq(1,popsize)
j<-seq(1,maxvar)
velocity[i,j]<<-.4* velocity[i,j] + 1 * runif(1) * (pbestsVar[i,j] - popVar[i,j]) + 1 * runif(1) * (archiveVar[gBest,j] - popVar[i,j])

Also does using lapply instead of a for loop increase performance?

many thanks

samden
January 17th, 2010, 04:09 PM
Don't know C so can't help with the main query, but I understand lapply should be considerably faster than a for loop. Having said that I still tend to use for loops for nearly everything... I'll need to improve that!