title: Weighted mean with aggregate function
body: <p>I want to get mean of <code>V1</code> weighted by <code>V2</code> and split by <code>V3</code> and <code>V4</code>, so I tried <code>aggregate</code>, it doesn't work because <code>weighted.mean</code> would only accept one parameter from <code>aggregate</code> so it's the same as <code>mean</code>:</p>
  
  <pre><code>df &lt;- data.frame(V1=seq(1, 1.5, 0.1),
                   V2=seq(6, 1, -1),
                   V3=c('A','A','A','B','B','B'),
                   V4=c('F','P','P','P','F','F'))
  aggregate(cbind(V1, V2) ~ V3 + V4, data=df, weighted.mean)
  
    V3 V4   V1  V2
  1  A  F 1.00 6.0
  2  B  F 1.45 1.5
  3  A  P 1.15 4.5
  4  B  P 1.30 3.0
  </code></pre>
  
  <p>So I have to go back to <code>split</code>,<code>lapply</code> and deal with many other stuffs</p>
  
  <pre><code>df1 &lt;- expand.grid(unique(df$V3), unique(df$V4))
  colnames(df1) &lt;- c('V3','V4')
  df1$CrossTerm &lt;- interaction(df1$V3, df1$V4)
  
  temp &lt;- sapply(split(df, interaction(df$V3, df$V4)), function(df) weighted.mean(df$V1, df$V2))
  df2 &lt;- data.frame(WeightedAvg=temp)
  df2$CrossTerm &lt;- rownames(df2)
  df3 &lt;- merge(df1, df2, by="CrossTerm")
  df3$CrossTerm &lt;- NULL
  df3
  
    V3 V4 WeightedAvg
  1  A  F    1.000000
  2  A  P    1.144444
  3  B  F    1.433333
  4  B  P    1.300000
  </code></pre>
  
  <p>But it's a ugly and possibly buggy solution, is there any better solution to get a similar result like this? The format is important and should be as similar to my output as possible(sequence of rows can be different).</p>