[R] Avoiding for loops

Maram SAlem marammagdysalem at gmail.com
Sun Oct 25 11:42:23 CET 2015


Hi All,

I wonder if I can avoid the for() loop in any of the following loops.These
loops are a part of a larger code which I'm trying to accelerate.

n=6
m=4
x<-c(0,1,1)

1st loop

for (i in 1:m-1)
   {
   d[i]<- n- (sum(x[(1):(i)])) - i
   }
  e<- n*(prod(d))


  2nd loop

LD<-list()
   for (i in 1:(m-1))
   {
   LD[[i]]<-seq(0,x[i],1)
   }

   LD[[m]]<-seq(0,(n-m-sum(x)),1)
   LED<-expand.grid (LD)
   LED<-as.matrix(LED)


3rd loop

for (i in 1:(m-1))

   {

    h[i]<- choose(x[i],LED[j,i])

     }



4th loop


 for (i in 1:(m-1))

  {

    lm[i]<-(sum(LED[j,1:i])) + i

      }


I appreciate if anyone has any suggestions or references.


Thanks in advance.


Maram Salem

	[[alternative HTML version deleted]]



More information about the R-help mailing list