title: Difference between filter and group_by when calculating change
body: <p>I'm trying to calculate percentage change but I don't get why this error occurs:</p>
  
  <p><code>Column 'change' must be length 1 (the group size), not 0</code></p>
  
  <p>Here's an <code>example</code> data frame:</p>
  
  <pre><code>structure(list(Kennisnamedatum = structure(c(16436, 16436, 16441, 
  17167, 17167, 17169), class = "Date"), weekdag = structure(c(4L, 
  4L, 2L, 7L, 7L, 2L), .Label = c("Mon", "Tue", "Wed", "Thu", "Fri", 
  "Sat", "Sun"), class = "factor"), jaar = c(2015L, 2015L, 2015L, 
  2017L, 2017L, 2017L), Gemeente = c("Amsterdam", "Rotterdam", 
  "S-Gravenhage", "Amsterdam", "S-Gravenhage", "Rotterdam"), Plaats = c("Amsterdam", 
  "Rotterdam", "S-Gravenhage", "Amsterdam", "S-Gravenhage", "Rotterdam"
  ), Beleidscode = c("Bezit harddrugs", "Bezit harddrugs", "Bezit harddrugs", 
  "Bezit harddrugs", "Bezit harddrugs", "Bezit harddrugs"), aantal_misdrijven = c(16L, 
  1L, 1L, 10L, 1L, 1L), misdrijven_per_jaar = c(829L, 368L, 205L, 
  649L, 197L, 349L)), class = c("grouped_df", "tbl_df", "tbl", 
  "data.frame"), row.names = c(NA, -6L), vars = c("jaar", "Gemeente", 
  "Beleidscode"), drop = TRUE, .Names = c("Kennisnamedatum", "weekdag", 
  "jaar", "Gemeente", "Plaats", "Beleidscode", "aantal_misdrijven", 
  "misdrijven_per_jaar"), indices = list(0L, 1L, 2L, 3L, 5L, 4L), group_sizes = c(1L, 
  1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
      jaar = c(2015L, 2015L, 2015L, 2017L, 2017L, 2017L), Gemeente = c("Amsterdam", 
      "Rotterdam", "S-Gravenhage", "Amsterdam", "Rotterdam", "S-Gravenhage"
      ), Beleidscode = c("Bezit harddrugs", "Bezit harddrugs", 
      "Bezit harddrugs", "Bezit harddrugs", "Bezit harddrugs", 
      "Bezit harddrugs")), class = "data.frame", row.names = c(NA, 
  -6L), vars = c("jaar", "Gemeente", "Beleidscode"), drop = TRUE, .Names = c("jaar", 
  "Gemeente", "Beleidscode")))
  </code></pre>
  
  <p>When I filter the data frame on cities (<code>gemeente</code>), Fellonies (<code>Beleidscode</code>) and leave two years (<code>jaar</code>) then I want to calculate change it works. But I don't want to filter but calculate across all (df: <code>wob_df</code>) <code>gemeente</code> per <code>Beleidscode</code> and per <code>jaar</code>. <code>group_by</code> would have been the obvious choice but that doesn't work. I can't wrap my head around this...</p>
  
  <p>Code that works (with original <code>wob_df</code>, too big a data frame to share):</p>
  
  <pre><code>library(dplyr)
  
  wob_df %&gt;%
    distinct(Gemeente, .keep_all = T) %&gt;%
    filter(Gemeente %in% c("Amsterdam", "Rotterdam", "S-Gravenhage"),
           Beleidscode == "Bezit harddrugs",
           jaar != 2016) %&gt;%
    group_by(Gemeente) %&gt;%
    mutate(change = (misdrijven_per_jaar[jaar == 2017] - misdrijven_per_jaar[jaar == 2015]) / misdrijven_per_jaar[jaar == 2015])
  </code></pre>
  
  <p>Same task but then without filter:</p>
  
  <pre><code>wob_df %&gt;%
    group_by(Gemeente, Beleidscode, jaar) %&gt;%
    mutate(change = (misdrijven_per_jaar[jaar == 2017] - misdrijven_per_jaar[jaar == 2015]) / misdrijven_per_jaar[jaar == 2015])
  </code></pre>
  
  <p>This gives the following error:</p>
  
  <pre><code>`Column 'change' must be length 1 (the group size), not 0`
  </code></pre>