[R] Odp: For loops in R

Petr PIKAL petr.pikal at precheza.cz
Mon Jan 18 12:09:10 CET 2010


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:



> 
> Hello
> 
> Sorry for the confusion
> Here are additional details.
> 
>  velocity, popVar, archiveVar,pbestsVar are matrices with dimension of 
500
> row, 2 col matrix.
> I want to assign new values for each velocity[i,j] using the formula 
below:
> 
> *0.4 * velocity[i,j] + 1 * randomvalue * (pbestsVar[i,j] - popVar[i,j]) 
+ 1
> * randomvalue * (archiveVar[random(1..500), j] -popVar[i,j]) *
> 
> with each random value different for each i j pair.
> 
> I want to do this without using the double for loops. I read somewhere 
that
> for loops take longer to execute in R. So I want to eliminate them for
> improved performance.
> 
> Long time C user and still adjusting to the R structures.
> 
> thanks for your help :)
> On Mon, Jan 18, 2010 at 4:28 PM, Petr Pikal [via R] <
> 
ml-node+1016444-458463297 at n4.nabble.com<ml-node%2B1016444-458463297 at n4.nabble.com>
> > wrote:
> 
> > Hi
> >
> > Definitely not
> >
> > from first I get
> >
> > > gBest<-floor(runif(popsize,min=1,max=top))
> > Error in runif(popsize, min = 1, max = top) : object 'popsize' not 
found
> > >
> >
> > and from second
> >
> > > 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])
> > 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
> >
> >
> > [hidden email]<
http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=0
> >napsal dne 17.01.2010 08:17:16:
> >
> > >
> > > 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
> > >
> > >
> > > --
> > > View this message in context:
> > http://n4.nabble.com/For-loops-in-R-tp1015933p1015933.html
> > > Sent from the R help mailing list archive at Nabble.com.
> > >
> > > ______________________________________________
> > > [hidden email]<
http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=1
> >mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html<http://www.r-project.org/
> posting-guide.html>
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> > ______________________________________________
> > [hidden email]<
http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=2
> >mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html<http://www.r-project.org/
> posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
> >
> > ------------------------------
> > View message @ 
http://n4.nabble.com/For-loops-in-R-tp1015933p1016444.html
> > To unsubscribe from For loops in R, click here< (link removed) =>.
> >
> >
> >
> 
> -- 
> View this message in context: 
http://n4.nabble.com/For-loops-in-R-tp1015933p1016473.html
> Sent from the R help mailing list archive at Nabble.com.
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org 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