title: How to speed up this function (for n parameters) in R?
body: <p>I have this function: </p>
  
  <pre><code>col &lt;- 0
  rres &lt;- data.frame(matrix(nrow=nrow(ind),ncol=length(lt)))
  gig &lt;- NULL
  &gt; lt
       [1]  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
  &gt; delta.1
   [1]   5   7   9  10  12  15  17  20  22  26  29  34  39  46  54  68  96 138 138
  
  
  
  &gt; f.bio
      function(x,y,a,b,l,k,m)
      {
         for (t in 1:nrow(y)){
          for (i in  1:length(lt)){
            for(j in 1:delta.1[i]){
  
          ifelse (t+j-1&gt;nrow(x),gig[j]&lt;-  NA,
                  gig[j] &lt;- x[t+j-1,i]*
                    (a*(l-(((l-(lt[i]+1))/(exp(-k*((j-1)/12))))))
                     ^b)*exp(m[(1+j),i]*(j-1)))    
  
        }
        rres[t,i] &lt;- sum(gig, na.rm = TRUE)
      }
      result &lt;- apply(rres,1,function(x) sum(x)/1000000)  
    }
     return(result)
  }
  </code></pre>
  
  <p>which it is apply to some biological data, the code is: </p>
  
  <pre><code>f.bio(ind,eff,a_all,b_all,Linf,K_coef,mort)
  </code></pre>
  
  <p>where the arguments are: </p>
  
  <pre><code>&gt; dim(ind)
  [1] 1356   19
  &gt; dim(eff)
  [1] 1356    1
  a_all = 0.004
  b_all= 3
  Linf= 19.4
  K_coef = 0.57
  &gt; dim(mort)
  [1] 110  19
  </code></pre>
  
  <p><code>ind</code>, <code>eff</code>, and <code>mort</code> are data.frame. 
  Now, my question is, is possible to apply this function to  <code>n</code> parameters, without excessive time machine?
  I mean for <code>n</code> parameters  a distribution of a certain parameters, for example: </p>
  
  <pre><code> set.seed(1) 
  a_all_v &lt;- round(sort(rnorm(40,a_all,0.00034)),5) #40 values!!
  </code></pre>
  
  <p>and so on for the <code>4 par</code>: <code>a_all, b_all, K_coef, Linf</code></p>
  
  <p>I wrote this code, with loop ( in this loop i can combine <code>a_all</code> with <code>b_all</code>, and <code>Linf</code> with <code>K_coef</code>): </p>
  
  <pre><code>col &lt;- 0
  for (m1 in 1:length(a_all_v)){ 
    a_all &lt;- a_all_v[m1]
     b_all &lt;- b_all_v[m1]
      for(m2 in 1:length(Linf_v)){ 
        Linf &lt;- Linf_v[m2]
         K_coef &lt;- k_coef_v[m2]
         col &lt;- col+1
           res.temp &lt;-f.bio(ind,eff,a_all,b_all,Linf,K_coef,mort)
            res.2[,col] &lt;-res.temp 
   }
  }
  </code></pre>
  
  <p>where res.2 is: </p>
  
  <pre><code>res.2 &lt;- data.frame(matrix(nrow=1356,ncol=1600)) #1600=40*40 (number of values for each parameters distribution)
  </code></pre>
  
  <p>This loop employ many time machine (many day on my PC). For these reason, there is some package or function (like Monte Carlo or bootstrap) that can change my code structure, and run the function with a good number of parameters combination, in little time (if is possible)? </p>