title: dcast with custom fun.aggregate
body: <p>I have data that looks like this:</p>
  
  <pre><code>sample start end gene coverage
  X      1     10  A    5
  X      11    20  A    10
  Y      1     10  A    5
  Y      11    20  A    10
  X      1     10  B    5
  X      11    20  B    10
  Y      1     10  B    5
  Y      11    20  B    10
  </code></pre>
  
  <p>I added additional columns:</p>
  
  <pre><code>data$length &lt;- (data$end - data$start + 1)
  
  data$ct_lt &lt;- (data$length * data$coverage)
  </code></pre>
  
  <p>I reformated my data using dcast:</p>
  
  <pre><code>casted &lt;- dcast(data, gene ~ sample, value.var = "coverage", fun.aggregate = mean)
  </code></pre>
  
  <p>So my new data looks like this:</p>
  
  <pre><code>gene    X       Y
  A      10.00000 10.00000
  B      38.33333 38.33333
  </code></pre>
  
  <p>This is the correct data format I desire, but I would like to fun.aggregate differently. Instead, I would like to take a weighted average, with coverage weighted by length:</p>
  
  <p>( sum (ct_lt) ) / ( sum ( length ) )</p>
  
  <p>How do I go about doing this?</p>