title: R: build data frame in user defined function
body: <p>I've run into a problem in defining a function in R. I'm trying to convert the contents of a <code>for</code> loop into a function, so that I can <code>lapply</code> the function to a list, (and in turn <code>mclapply</code> using the <code>multicore</code> package)</p>
  
  <p>I need to build a data frame row by row, taking one text and one numerical value at a time from the function. I understand from a previous post that I must return the values somehow, because of R's pass-and-copy rather than pass-by-reference scheme (see: <a href="https://stackoverflow.com/questions/6359104/how-to-add-a-column-in-the-data-frame-within-a-function">How to add a column in the data frame within a function</a></p>
  
  <p>Here's a simplified version of what I'm doing:</p>
  
  <pre><code>letters &lt;- c('aa', 'bb', 'cc', 'aa', 'bb', 'dd', 'cc', 'dd', 'ee', 'ee')
  numbers &lt;- 1:10
  madeup &lt;- data.frame(letters, numbers)
  output &lt;- data.frame()
  
  functest &lt;- function(x) {
      subtest &lt;- subset(madeup, letters==x)
      if (nrow(subtest==2)){
          calc &lt;- subtest$numbers[2] - subtest$numbers[1]
          ...WHAT GOES HERE?...
      }
  }
  </code></pre>
  
  <p>Anyone know what I should do to return <code>x</code> and <code>calc</code> so that I can then <code>rbind</code> these two values each into a new row of the <code>output</code> data frame?</p>
  
  <p>If I can copy these two values from the function, I'm imagining the execution command will look something like ---</p>
  
  <pre><code>output &lt;- rbind(output, [mc]lapply(letters, functest))
  </code></pre>
  
  <p>--- but am happy to be corrected on this!</p>