Skip to content

For loops in R

6 messages · cjmr, PIKAL Petr

#
Hello.

I've just started using R and am trying to figure out if the two codes
snippets below have the same output

gBest<-floor(runif(popsize,min=1,max=top))
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 

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])
			#velocity[i,j]<-.4* velocity[i,j] + 1 * .3722 * (pbestsVar[i,j] -
popVar[i,j]) + 1 * .3722 * (archiveVar[3,j] - popVar[i,j])
		}

many thanks
1 day later
#
Hi

Definitely not

from first I get
Error in runif(popsize, min = 1, max = top) : object 'popsize' not found
and from second
+   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])
+    #velocity[i,j]<-.4* velocity[i,j] + 1 * .3722 * (pbestsVar[i,j] -
+ popVar[i,j]) + 1 * .3722 * (archiveVar[3,j] - popVar[i,j])
Error: unexpected ')' in:
"   #velocity[i,j]<-.4* velocity[i,j] + 1 * .3722 * (pbestsVar[i,j] -
popVar[i,j])"
If you by chance have all data necessary for those codes not to give those 
stupid errors you could transfer their output to different objects and 
check those objects if they are equivalent.

?all.equal

Regards
Petr


r-help-bounces at r-project.org napsal dne 17.01.2010 08:17:16:
(pbestsVar[i,j] -
-
http://n4.nabble.com/For-loops-in-R-tp1015933p1015933.html
http://www.R-project.org/posting-guide.html
#
Hi

slightly better. However it would be nice if you provided some code which 
works but not the way you want. Loops are not as problematic but often you 
achieve far better results by avoiding them and using vectorised code.

Still there are some oddities.

random values in formula below shall be the same?
random(1:500) means arbitrary value from range 1:500? If yes shall be in 
each cycle different or they could be repeated?

What I would do is

I make 500x2 random matrix

rmat <- matrix(runif(1000), 500,2)

and random index

rindex <- sample(1:500)

0.4 * velocity + rmat * (pbestsVar - popVar) + rmat * (archiveVar[rindex, 
] - popVar)

shall result in a 500x2 matrix

But I am not sure if it is what you want as you did not provide enough 
clues.

Regards
Petr

r-help-bounces at r-project.org napsal dne 18.01.2010 10:08:10:
500
below:
+ 1
that
ml-node+1016444-458463297 at n4.nabble.com<ml-node%2B1016444-458463297 at n4.nabble.com>
found
(pbestsVar[i,j] -
those
http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=0
codes
(pbestsVar[i,j]
http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=1
http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=2
http://n4.nabble.com/For-loops-in-R-tp1015933p1016444.html
http://n4.nabble.com/For-loops-in-R-tp1015933p1016473.html
http://www.R-project.org/posting-guide.html
#
Hello Petr.

For the random values, I wanted to generate a different random number for
each element of my velocity matrix.

So will this do it?

rmat <- matrix(runif(1000), 500,2)
rmat2 <- matrix(runif(1000), 500,2)
rindex <- sample(1:500, replace=TRUE) #with repetition
velocity<-0.4 * velocity + rmat * (pbestsVar - popVar) + rmat2 *
(archiveVar[rindex,] - popVar)

Also, do the apply methods perform better than for loops given the same
function?
sample:
apply(x, fun) 
and 
for (i in 1:length(x)) fun(x[i])

cheers
cjmr
#
Hi

r-help-bounces at r-project.org napsal dne 19.01.2010 02:32:45:
for
AFAICS it seems to do what you want. Basically your rmat and rmat2 will 
have different numbers from uniform distribution. If you wanted different 
distribution of random numbers you need to use differend random number 
generator. See ?rnorm.

And repetition means that in your index you can have some rows more times 
and some rows never. Again only you know if this is desired behaviour. If 
not use replace=FALSE.
In some quite recent R-News (I believe 2009 or 2008) there is an article 
about loops. And also R-Inferno by P.Burns is worth reading.

My opinion is that simple cycles and apply functions are more or less 
similar. When I want to scan a file and make same plots for each column I 
use for cycle and in most other cases I use *apply family. If you have 
nested cycles which work correctly in reasonable time why not use them. 
But usually vectorised approach can be far quicker and, when you get used 
to it, clearer.

Regards
Petr
http://n4.nabble.com/For-loops-in-R-tp1015933p1017196.html
http://www.R-project.org/posting-guide.html