title: group by in R, ddply with weighted.mean
body: <p>I am trying to do a "group by" - style weighted mean in R. With some basic mean the following code (using the plyr package from Hadley) worked well. </p>
  
  <pre><code>ddply(mydf,.(period),mean)
  </code></pre>
  
  <p>If I use the same approach with weighted.mean i get the following error "'x' and 'w' must have the same length" , which I do not understand because the weighted.mean part works outside ddply. </p>
  
  <pre><code>weighted.mean(mydf$mycol,mydf$myweight) # works just fine
  ddply(mydf,.(period),weighted.mean,mydf$mycol,mydf$myweight) # returns the erros described above
  ddply(mydf,.(period),weighted.mean(mydf$mycol,mydf$myweight)) # different code same story
  </code></pre>
  
  <p>I thought of writing a custom function instead of using weighted.mean and then passing it to ddply or even writing something new from scratch with subset. In my case it would be too much work hopefully, but there should by a smarter solution with what´s already there. </p>
  
  <p>thx for any suggestions in advance!</p>