title: Count or sum the values of 2 columns based on the value of another column in the same dataframe
body: <p>I have the dataframe below: </p>
  
  <pre><code>year&lt;-c("2000","2000","2001","2002","2000","2002")
  gender&lt;-c("M","F","M","F","M","M")
  weight&lt;-c(0.5,0.7,0.8,0.7,0.6,0.9)
  YG&lt;-data.frame(year,gender,weight)
  </code></pre>
  
  <p>and I want to count the <code>gender</code> for years 2000 and 2001 and sum the <code>weight</code> for year 2002 in order to create a new dataframe like:</p>
  
  <pre><code>year   M   F
  1 2000 2.0 1.0
  2 2001 1.0 0.0
  3 2002 0.9 0.7
  </code></pre>
  
  <p>I tried something like:</p>
  
  <pre><code>library(tidyverse)
  YG %&gt;%
    group_by(year) %&gt;%
    summarise(sum(weight[year=="2002"]))%&gt;%
    count(round(gender[year!="2002"])) %&gt;%
    spread(gender, n, fill = 0)
  </code></pre>