title: R: Use lapply with a function acting on part of a split dataframe
body: <p>This R code is setting up an example of the issue I am attempting to resolve.  The data set measures a release of particles over non-uniform time intervals. The particle release is integrated over time using the trapezoid rule.     </p>
  
  <pre><code>library(caTools)
  
  test.data.frame &lt;- data.frame( 
      sample = c('sample 1','sample 1','sample 1','sample 1',
                 'sample 2','sample 2','sample 2','sample 2'))
  test.data.frame$time &lt;- c(1,2,4,6,1,4,5,6)
  test.data.frame$material.released.g &lt;- c(5,3,2,1,2,4,5,1)
  
  split.test &lt;- split(test.data.frame, test.data.frame$sample)
  
  integrate.test &lt;- function(x){
     dataframe.segment &lt;- do.call(rbind.data.frame,x)
     return(trapz(dataframe.segment$time,dataframe.segment$material.released.g))
  }
  </code></pre>
  
  <p>So far the integrate.test function appears to work on a single element of a list.</p>
  
  <pre><code>   &gt; integrate.test(split.test[1])
     [1] 12
  
     &gt; integrate.test(split.test[2])
     [1] 16.5
  </code></pre>
  
  <p>The lapply function gives zeros in the output.</p>
  
  <pre><code>  &gt; lapply(split.test, integrate.test)
    $`sample 1`
    [1] 0
  
    $`sample 2`
    [1] 0
  </code></pre>
  
  <p>The output I am looking for is a data frame equivalent to:</p>
  
  <pre><code>expected.output &lt;- data.frame(
      sample = c('sample 1','sample 2'), 
      total.material.released = c(12 , 16.5))
  </code></pre>
  
  <p>Is anyone able to help resolve the error code. Thanks!</p>