title: How to use ddply to add a column to a data frame?
body: <p>I have a data frame that looks like this:</p>
  
  <pre><code>site   date  var   dil
     1    A    7.4   2 
     2    A    6.5   2
     1    A    7.3   3
     2    A    7.3   3
     1    B    7.1   1
     2    B    7.7   2
     1    B    7.7   3
     2    B    7.4   3
  </code></pre>
  
  <p>I need add a column called <code>wt</code> to this dataframe that contains the weighting factor needed to calculate the weighted mean. This weighting factor has to be derived for each combination of <code>site</code> and <code>date</code>.</p>
  
  <p>The approach I'm using is to first built a function that calculate the weigthing factor:</p>
  
  <pre><code>&gt; weight &lt;- function(dil){
                      dil/sum(dil)
                       }
  </code></pre>
  
  <p>then apply the function for each combination of <code>site</code> and <code>date</code></p>
  
  <pre><code>&gt; df$wt &lt;- ddply(df,.(date,site),.fun=weight)
  </code></pre>
  
  <p>but I get this error message:</p>
  
  <pre><code>Error in FUN(X[[1L]], ...) : 
    only defined on a data frame with all numeric variables
  </code></pre>