title: R Convert loop into function
body: <p>I would like to clean up my code a bit and start to use more functions for my everyday computations (where I would normally use for loops). I have an example of a for loop that I would like to make into a function. The problem I am having is in how to step through the constraint vectors without a loop. Here's what I mean;</p>
  <pre><code>## represents spectral data
  set.seed(11)
  df &lt;- data.frame(Sample = 1:100, replicate(1000, sample(0:1000, 100, rep = TRUE)))
  
  ## feature ranges by column number
  frm &lt;- c(438,563,953,963)
  to &lt;- c(548,803,1000,993)
  nm &lt;- c(&quot;WL890&quot;, &quot;WL1080&quot;, &quot;WL1400&quot;, &quot;WL1375&quot;)
  
  WL.ps &lt;- list()
  
  for (i in 1:length(frm)){
    
    ## finds the minimum value within the range constraints and returns the corresponding column name  
    WL &lt;- colnames(df[frm[i]:to[i]])[apply(df[frm[i]:to[i]],1,which.min)]
    
    WL.ps[[i]] &lt;- WL
  }
  
  new.df &lt;- data.frame(WL.ps)
  colnames(new.df) &lt;- nm
  
  </code></pre>
  <p>The part where I iterate through the 'frm' and 'to' vector values is what I'm having trouble with. How does one go from frm[1] to frm[2].. so-on in a function (apply or otherwise)?</p>
  <p>Any advice would be greatly appreciated.</p>
  <p>Thank you.</p>