title: lapply with optional function argument and optional vector arguments
body: <p>I have a function in R, say<br>
  f1 &lt;- function(x,y,vec, func0,...){<br>
  ...<br>
  ...<br>
  ...<br>
  return(out)
  }</p>
  
  <p>The arguments func0 and vec in this function f1 are function object and some vector object respectively. Now I want to repeat this function 'reps' times (everything else being the same). I have stored the arguments of this function in a list as there are a lot of arguments and I keep changing them to do the replications again.</p>
  
  <p>list1 &lt;- list(x,y,vec, func0, other arguments)</p>
  
  <p>Then I want to do,
  f1_reps &lt;- lapply(1:reps, f1, list1)</p>
  
  <p>I get an error when I do this as the function arguments func0 and vec are not found.</p>
  
  <p>Any help in this direction would be helpful. Here is a mock example of the situation.</p>
  
  <p>Here is an example,</p>
  
  <pre><code>a &lt;- function(n){
    sqrt(n)
  }
  N = 100
  out &lt;- rep(NA,N)
  # simple function with multiple arguments
  foo &lt;- function(a=a, b= c(1:3), c= 1000){
    for(n in 1:N){
      out[n] &lt;- b%*%b+ a(n)*c
    }
    return(out)
  }
  
  candidates &lt;- list(a=a, b = c(1:3), c=1000)
  lapply(1:4, foo(a=candidates$a,b=candidates$b,c=candidates$c))  ## Doesn't work
  lapply(1:4, foo, a=candidates$a, b=candidates$b, c=candidates$c) ## Doesn't work
  
  candidates2 &lt;- c(a=a, b = c(1:3), c=1000) # A vector of arguments
  lapply(1:4, foo, a=candidates2$a, b = c(candidates2$b1,candidates2$b2,candidates2$b3), c=candidates2$c) #Doesn't work either
  
  </code></pre>