title: calculated weighted average in r based on two columns
body: <p>I have a data frame as follows: </p>
  
  <pre><code>date              Rank         new_Weight       c
  2019-01-01         20           2               10
  2019-01-01         30           5               10 
  2019-01-01         10           8               10
  2019-02-02          3           10               60
  2019-02-02          5            2               60
  ....               ...          ....
  </code></pre>
  
  <p>I want to calculate the weighted average based on Rank and new weight 
  I have applied the following code: </p>
  
  <pre><code>by(df, df$date,subset) function(x){
    x&lt;-df$rank*df$new_weight/sum(df$new_weigth)
  }
  </code></pre>
  
  <p>and create a new column. </p>
  
  <p>I wrote the following function and it works very well. </p>
  
  <pre><code>df&lt;- df %&gt;% group_by(date) %&gt;% mutate(w=weighted.mean(rank,new_weight))
  </code></pre>
  
  <p>however I am wondering why the first function does not work. </p>