title: Aggregate and Weighted Mean in R
body: <p>I'm trying to calculate asset-weighted returns by asset class. For the life of me, I can't figure out how to do it using the aggregate command. </p>
  
  <p>My data frame looks like this </p>
  
  <pre><code>dat &lt;- data.frame(company, fundname, assetclass, return, assets)
  </code></pre>
  
  <p>I'm trying to do something like (don't copy this, it's wrong): </p>
  
  <pre><code>aggregate(dat, list(dat$assetclass), weighted.mean, w=(dat$return, dat$assets))
  </code></pre>